Search in sources :

Example 21 with PrestoException

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

the class HiveCoercionRecordCursor method createCoercer.

private static Coercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType) {
    Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
    Type toType = typeManager.getType(toHiveType.getTypeSignature());
    if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
        return new IntegerNumberToVarcharCoercer();
    } else if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
        return new VarcharToIntegerNumberCoercer(toHiveType);
    } else if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
        return new FloatToDoubleCoercer();
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) PrestoException(com.facebook.presto.spi.PrestoException)

Example 22 with PrestoException

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

the class HiveLocationService method forNewTable.

@Override
public LocationHandle forNewTable(SemiTransactionalHiveMetastore metastore, String user, String queryId, String schemaName, String tableName) {
    Path targetPath = getTableDefaultLocation(user, metastore, hdfsEnvironment, schemaName, tableName);
    // verify the target directory for the table
    if (pathExists(user, hdfsEnvironment, targetPath)) {
        throw new PrestoException(HIVE_PATH_ALREADY_EXISTS, format("Target directory for table '%s.%s' already exists: %s", schemaName, tableName, targetPath));
    }
    Path writePath;
    if (shouldUseTemporaryDirectory(user, targetPath)) {
        writePath = createTemporaryPath(user, hdfsEnvironment, targetPath);
    } else {
        writePath = targetPath;
    }
    return new LocationHandle(targetPath, Optional.of(writePath), false);
}
Also used : HiveWriteUtils.createTemporaryPath(com.facebook.presto.hive.HiveWriteUtils.createTemporaryPath) Path(org.apache.hadoop.fs.Path) PrestoException(com.facebook.presto.spi.PrestoException)

Example 23 with PrestoException

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

the class HiveMetadata method writeEmptyFile.

private static void writeEmptyFile(Path target, JobConf conf, Properties properties, String serDe, String outputFormatName) {
    // Some serializers such as Avro set a property in the schema.
    initializeSerializer(conf, properties, serDe);
    // The code below is not a try with resources because RecordWriter is not Closeable.
    FileSinkOperator.RecordWriter recordWriter = HiveWriteUtils.createRecordWriter(target, conf, properties, outputFormatName);
    try {
        recordWriter.close(false);
    } catch (IOException e) {
        throw new PrestoException(HIVE_WRITER_CLOSE_ERROR, "Error write empty file to Hive", e);
    }
}
Also used : FileSinkOperator(org.apache.hadoop.hive.ql.exec.FileSinkOperator) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException)

Example 24 with PrestoException

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

the class HiveUtil method getPartitionKeyColumnHandles.

public static List<HiveColumnHandle> getPartitionKeyColumnHandles(String connectorId, Table table) {
    ImmutableList.Builder<HiveColumnHandle> columns = ImmutableList.builder();
    List<Column> partitionKeys = table.getPartitionColumns();
    for (Column field : partitionKeys) {
        HiveType hiveType = field.getType();
        if (!hiveType.isSupportedType()) {
            throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type %s found in partition keys of table %s.%s", hiveType, table.getDatabaseName(), table.getTableName()));
        }
        columns.add(new HiveColumnHandle(connectorId, field.getName(), hiveType, hiveType.getTypeSignature(), -1, PARTITION_KEY, field.getComment()));
    }
    return columns.build();
}
Also used : Column(com.facebook.presto.hive.metastore.Column) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException)

Example 25 with PrestoException

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

the class HiveMetadata method getTableMetadata.

private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName) {
    Optional<Table> table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
    if (!table.isPresent() || table.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
        throw new TableNotFoundException(tableName);
    }
    Function<HiveColumnHandle, ColumnMetadata> metadataGetter = columnMetadataGetter(table.get(), typeManager);
    ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
    for (HiveColumnHandle columnHandle : hiveColumnHandles(connectorId, table.get())) {
        columns.add(metadataGetter.apply(columnHandle));
    }
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
    if (table.get().getTableType().equals(EXTERNAL_TABLE.name())) {
        properties.put(EXTERNAL_LOCATION_PROPERTY, table.get().getStorage().getLocation());
    }
    try {
        HiveStorageFormat format = extractHiveStorageFormat(table.get());
        properties.put(STORAGE_FORMAT_PROPERTY, format);
    } catch (PrestoException ignored) {
    // todo fail if format is not known
    }
    List<String> partitionedBy = table.get().getPartitionColumns().stream().map(Column::getName).collect(toList());
    if (!partitionedBy.isEmpty()) {
        properties.put(PARTITIONED_BY_PROPERTY, partitionedBy);
    }
    Optional<HiveBucketProperty> bucketProperty = table.get().getStorage().getBucketProperty();
    if (bucketProperty.isPresent()) {
        properties.put(BUCKET_COUNT_PROPERTY, bucketProperty.get().getBucketCount());
        properties.put(BUCKETED_BY_PROPERTY, bucketProperty.get().getBucketedBy());
    }
    properties.putAll(tableParameterCodec.decode(table.get().getParameters()));
    return new ConnectorTableMetadata(tableName, columns.build(), properties.build());
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Table(com.facebook.presto.hive.metastore.Table) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) HiveTableProperties.getHiveStorageFormat(com.facebook.presto.hive.HiveTableProperties.getHiveStorageFormat) StorageFormat.fromHiveStorageFormat(com.facebook.presto.hive.metastore.StorageFormat.fromHiveStorageFormat) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

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