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());
}
}
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);
}
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));
}
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);
}
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));
}
Aggregations