Search in sources :

Example 11 with PrestoException

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

the class TestingHiveMetastore method createTable.

@Override
public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) {
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
    SchemaTableName schemaTableName = new SchemaTableName(table.getDatabaseName(), table.getTableName());
    if (tableType == VIRTUAL_VIEW) {
        checkArgument(table.getStorage().getLocation().isEmpty(), "Storage location for view must be empty");
    } else {
        File directory = new File(URI.create(table.getStorage().getLocation()));
        if (!directory.exists()) {
            throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory does not exist");
        }
        if (tableType == MANAGED_TABLE && !isParentDir(directory, baseDirectory)) {
            throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory must be inside of the metastore base directory");
        }
    }
    if (relations.putIfAbsent(schemaTableName, table) != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Collection(java.util.Collection) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) File(java.io.File)

Example 12 with PrestoException

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

the class TestingHiveMetastore method addColumn.

@Override
public synchronized void addColumn(String databaseName, String tableName, String columnName, HiveType columnType, String columnComment) {
    SchemaTableName name = new SchemaTableName(databaseName, tableName);
    Table oldTable = getRequiredTable(name);
    if (oldTable.getColumn(columnName).isPresent()) {
        throw new PrestoException(ALREADY_EXISTS, "Column already exists: " + columnName);
    }
    Table newTable = Table.builder(oldTable).addDataColumn(new Column(columnName, columnType, Optional.ofNullable(columnComment))).build();
    relations.put(name, newTable);
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 13 with PrestoException

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

the class TestingHiveMetastore method alterPartition.

@Override
public synchronized void alterPartition(String databaseName, String tableName, Partition partition) {
    PartitionName partitionName = new PartitionName(databaseName, tableName, partition.getValues());
    Partition oldPartition = partitions.get(partitionName);
    if (oldPartition == null) {
        throw new PartitionNotFoundException(new SchemaTableName(databaseName, tableName), partition.getValues());
    }
    if (!oldPartition.getStorage().getLocation().equals(partition.getStorage().getLocation())) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "alterPartition can not change storage location");
    }
    dropPartition(databaseName, tableName, partition.getValues(), false);
    addPartitions(databaseName, tableName, ImmutableList.of(partition));
}
Also used : PartitionNotFoundException(com.facebook.presto.hive.PartitionNotFoundException) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 14 with PrestoException

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

the class TestingHiveMetastore method renameColumn.

@Override
public synchronized void renameColumn(String databaseName, String tableName, String oldColumnName, String newColumnName) {
    SchemaTableName name = new SchemaTableName(databaseName, tableName);
    Table oldTable = getRequiredTable(name);
    if (oldTable.getColumn(newColumnName).isPresent()) {
        throw new PrestoException(ALREADY_EXISTS, "Column already exists: " + newColumnName);
    }
    if (!oldTable.getColumn(oldColumnName).isPresent()) {
        throw new ColumnNotFoundException(name, oldColumnName);
    }
    for (Column column : oldTable.getPartitionColumns()) {
        if (column.getName().equals(oldColumnName)) {
            throw new PrestoException(NOT_SUPPORTED, "Renaming partition columns is not supported");
        }
    }
    ImmutableList.Builder<Column> newDataColumns = ImmutableList.builder();
    for (Column fieldSchema : oldTable.getDataColumns()) {
        if (fieldSchema.getName().equals(oldColumnName)) {
            newDataColumns.add(new Column(newColumnName, fieldSchema.getType(), fieldSchema.getComment()));
        } else {
            newDataColumns.add(fieldSchema);
        }
    }
    Table newTable = Table.builder(oldTable).setDataColumns(newDataColumns.build()).build();
    relations.put(name, newTable);
}
Also used : ColumnNotFoundException(com.facebook.presto.spi.ColumnNotFoundException) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 15 with PrestoException

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

the class OrcTester method preprocessWriteValueOld.

private static Object preprocessWriteValueOld(TypeInfo typeInfo, Object value) {
    if (value == null) {
        return null;
    }
    switch(typeInfo.getCategory()) {
        case PRIMITIVE:
            PrimitiveObjectInspector.PrimitiveCategory primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
            switch(primitiveCategory) {
                case BOOLEAN:
                    return value;
                case BYTE:
                    return ((Number) value).byteValue();
                case SHORT:
                    return ((Number) value).shortValue();
                case INT:
                    return ((Number) value).intValue();
                case LONG:
                    return ((Number) value).longValue();
                case FLOAT:
                    return ((Number) value).floatValue();
                case DOUBLE:
                    return ((Number) value).doubleValue();
                case DECIMAL:
                    return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
                case STRING:
                    return value;
                case CHAR:
                    return new HiveChar(value.toString(), ((CharTypeInfo) typeInfo).getLength());
                case DATE:
                    int days = ((SqlDate) value).getDays();
                    LocalDate localDate = LocalDate.ofEpochDay(days);
                    ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
                    long millis = zonedDateTime.toEpochSecond() * 1000;
                    Date date = new Date(0);
                    // mills must be set separately to avoid masking
                    date.setTime(millis);
                    return date;
                case TIMESTAMP:
                    long millisUtc = (int) ((SqlTimestamp) value).getMillisUtc();
                    return new Timestamp(millisUtc);
                case BINARY:
                    return ((SqlVarbinary) value).getBytes();
            }
            break;
        case MAP:
            MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
            TypeInfo keyTypeInfo = mapTypeInfo.getMapKeyTypeInfo();
            TypeInfo valueTypeInfo = mapTypeInfo.getMapValueTypeInfo();
            Map<Object, Object> newMap = new HashMap<>();
            for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                newMap.put(preprocessWriteValueOld(keyTypeInfo, entry.getKey()), preprocessWriteValueOld(valueTypeInfo, entry.getValue()));
            }
            return newMap;
        case LIST:
            ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
            TypeInfo elementTypeInfo = listTypeInfo.getListElementTypeInfo();
            List<Object> newList = new ArrayList<>(((Collection<?>) value).size());
            for (Object element : (Iterable<?>) value) {
                newList.add(preprocessWriteValueOld(elementTypeInfo, element));
            }
            return newList;
        case STRUCT:
            StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
            List<?> fieldValues = (List<?>) value;
            List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
            List<Object> newStruct = new ArrayList<>();
            for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
                newStruct.add(preprocessWriteValueOld(fieldTypeInfos.get(fieldId), fieldValues.get(fieldId)));
            }
            return newStruct;
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo));
}
Also used : HashMap(java.util.HashMap) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) ArrayList(java.util.ArrayList) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrestoException(com.facebook.presto.spi.PrestoException) LocalDate(java.time.LocalDate) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) Timestamp(java.sql.Timestamp) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ZonedDateTime(java.time.ZonedDateTime) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) SqlDate(com.facebook.presto.spi.type.SqlDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) SqlDate(com.facebook.presto.spi.type.SqlDate) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Aggregations

PrestoException (com.facebook.presto.spi.PrestoException)747 IOException (java.io.IOException)161 ImmutableList (com.google.common.collect.ImmutableList)106 Type (com.facebook.presto.common.type.Type)95 SchemaTableName (com.facebook.presto.spi.SchemaTableName)88 List (java.util.List)83 ArrayList (java.util.ArrayList)79 Path (org.apache.hadoop.fs.Path)78 Optional (java.util.Optional)68 ImmutableMap (com.google.common.collect.ImmutableMap)64 Map (java.util.Map)64 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)58 Block (com.facebook.presto.common.block.Block)55 Table (com.facebook.presto.hive.metastore.Table)55 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)55 Objects.requireNonNull (java.util.Objects.requireNonNull)55 Slice (io.airlift.slice.Slice)50 SqlType (com.facebook.presto.spi.function.SqlType)49 Test (org.testng.annotations.Test)49 ConnectorSession (com.facebook.presto.spi.ConnectorSession)46