Search in sources :

Example 1 with NoSuchTableException

use of org.apache.iceberg.exceptions.NoSuchTableException in project presto by prestodb.

the class IcebergHadoopMetadata method getSystemTable.

@Override
public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTableName tableName) {
    IcebergTableName name = IcebergTableName.from(tableName.getTableName());
    TableIdentifier tableIdentifier = toIcebergTableIdentifier(tableName.getSchemaName(), name.getTableName());
    Table table;
    try {
        table = resourceFactory.getCatalog(session).loadTable(tableIdentifier);
    } catch (NoSuchTableException e) {
        return Optional.empty();
    }
    if (name.getSnapshotId().isPresent() && table.snapshot(name.getSnapshotId().get()) == null) {
        throw new PrestoException(ICEBERG_INVALID_SNAPSHOT_ID, format("Invalid snapshot [%s] for table: %s", name.getSnapshotId().get(), table));
    }
    return getIcebergSystemTable(tableName, table);
}
Also used : TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) IcebergPrestoModelConverters.toIcebergTableIdentifier(com.facebook.presto.iceberg.util.IcebergPrestoModelConverters.toIcebergTableIdentifier) SystemTable(com.facebook.presto.spi.SystemTable) IcebergUtil.getHadoopIcebergTable(com.facebook.presto.iceberg.IcebergUtil.getHadoopIcebergTable) Table(org.apache.iceberg.Table) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) PrestoException(com.facebook.presto.spi.PrestoException)

Example 2 with NoSuchTableException

use of org.apache.iceberg.exceptions.NoSuchTableException in project presto by prestodb.

the class IcebergHadoopMetadata method getTableMetadata.

@Override
protected ConnectorTableMetadata getTableMetadata(ConnectorSession session, SchemaTableName table) {
    Table icebergTable;
    try {
        icebergTable = getHadoopIcebergTable(resourceFactory, session, table);
    } catch (NoSuchTableException e) {
        throw new TableNotFoundException(table);
    }
    List<ColumnMetadata> columns = getColumnMetadatas(icebergTable);
    return new ConnectorTableMetadata(table, columns, createMetadataProperties(icebergTable), getTableComment(icebergTable));
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SystemTable(com.facebook.presto.spi.SystemTable) IcebergUtil.getHadoopIcebergTable(com.facebook.presto.iceberg.IcebergUtil.getHadoopIcebergTable) Table(org.apache.iceberg.Table) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Example 3 with NoSuchTableException

use of org.apache.iceberg.exceptions.NoSuchTableException in project hive by apache.

the class HiveIcebergMetaHook method rollbackAlterTable.

@Override
public void rollbackAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, EnvironmentContext context) {
    if (Boolean.parseBoolean(context.getProperties().getOrDefault(MIGRATE_HIVE_TO_ICEBERG, "false"))) {
        LOG.debug("Initiating rollback for table {} at location {}", hmsTable.getTableName(), hmsTable.getSd().getLocation());
        context.getProperties().put(INITIALIZE_ROLLBACK_MIGRATION, "true");
        this.catalogProperties = getCatalogProperties(hmsTable);
        try {
            this.icebergTable = Catalogs.loadTable(conf, catalogProperties);
        } catch (NoSuchTableException nte) {
            // iceberg table was not yet created, no need to delete the metadata dir separately
            return;
        }
        // we want to keep the data files but get rid of the metadata directory
        String metadataLocation = ((BaseTable) this.icebergTable).operations().current().metadataFileLocation();
        try {
            Path path = new Path(metadataLocation).getParent();
            FileSystem.get(path.toUri(), conf).delete(path, true);
            LOG.debug("Metadata directory of iceberg table {} at location {} was deleted", icebergTable.name(), path);
        } catch (IOException e) {
        // the file doesn't exists, do nothing
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) BaseTable(org.apache.iceberg.BaseTable) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) IOException(java.io.IOException)

Example 4 with NoSuchTableException

use of org.apache.iceberg.exceptions.NoSuchTableException in project hive by apache.

the class HiveCatalog method renameTable.

@Override
public void renameTable(TableIdentifier from, TableIdentifier originalTo) {
    if (!isValidIdentifier(from)) {
        throw new NoSuchTableException("Invalid identifier: %s", from);
    }
    TableIdentifier to = removeCatalogName(originalTo);
    Preconditions.checkArgument(isValidIdentifier(to), "Invalid identifier: %s", to);
    String toDatabase = to.namespace().level(0);
    String fromDatabase = from.namespace().level(0);
    String fromName = from.name();
    try {
        Table table = clients.run(client -> client.getTable(fromDatabase, fromName));
        HiveTableOperations.validateTableIsIceberg(table, fullTableName(name, from));
        table.setDbName(toDatabase);
        table.setTableName(to.name());
        clients.run(client -> {
            client.alter_table(fromDatabase, fromName, table);
            return null;
        });
        LOG.info("Renamed table from {}, to {}", from, to);
    } catch (NoSuchObjectException e) {
        throw new NoSuchTableException("Table does not exist: %s", from);
    } catch (AlreadyExistsException e) {
        throw new org.apache.iceberg.exceptions.AlreadyExistsException("Table already exists: %s", to);
    } catch (TException e) {
        throw new RuntimeException("Failed to rename " + from + " to " + to, e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Interrupted in call to rename", e);
    }
}
Also used : TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) TException(org.apache.thrift.TException) Table(org.apache.hadoop.hive.metastore.api.Table) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException)

Example 5 with NoSuchTableException

use of org.apache.iceberg.exceptions.NoSuchTableException in project hive by apache.

the class HiveIcebergSerDe method initialize.

@Override
public void initialize(@Nullable Configuration configuration, Properties serDeProperties, Properties partitionProperties) throws SerDeException {
    super.initialize(configuration, serDeProperties, partitionProperties);
    if (serDeProperties.get(InputFormatConfig.TABLE_SCHEMA) != null) {
        this.tableSchema = SchemaParser.fromJson((String) serDeProperties.get(InputFormatConfig.TABLE_SCHEMA));
        if (serDeProperties.get(InputFormatConfig.PARTITION_SPEC) != null) {
            PartitionSpec spec = PartitionSpecParser.fromJson(tableSchema, serDeProperties.getProperty(InputFormatConfig.PARTITION_SPEC));
            this.partitionColumns = spec.fields().stream().map(PartitionField::name).collect(Collectors.toList());
        } else {
            this.partitionColumns = ImmutableList.of();
        }
    } else {
        try {
            Table table = IcebergTableUtil.getTable(configuration, serDeProperties);
            // always prefer the original table schema if there is one
            this.tableSchema = table.schema();
            this.partitionColumns = table.spec().fields().stream().map(PartitionField::name).collect(Collectors.toList());
            LOG.info("Using schema from existing table {}", SchemaParser.toJson(tableSchema));
        } catch (Exception e) {
            // During table creation we might not have the schema information from the Iceberg table, nor from the HMS
            // table. In this case we have to generate the schema using the serdeProperties which contains the info
            // provided in the CREATE TABLE query.
            boolean autoConversion = configuration.getBoolean(InputFormatConfig.SCHEMA_AUTO_CONVERSION, false);
            // If we can not load the table try the provided hive schema
            this.tableSchema = hiveSchemaOrThrow(e, autoConversion);
            // This is only for table creation, it is ok to have an empty partition column list
            this.partitionColumns = ImmutableList.of();
            // create table for CTAS
            if (e instanceof NoSuchTableException && Boolean.parseBoolean(serDeProperties.getProperty(hive_metastoreConstants.TABLE_IS_CTAS))) {
                if (!Catalogs.hiveCatalog(configuration, serDeProperties)) {
                    throw new SerDeException(CTAS_EXCEPTION_MSG);
                }
                createTableForCTAS(configuration, serDeProperties);
            }
        }
    }
    Schema projectedSchema;
    if (serDeProperties.get(HiveIcebergStorageHandler.WRITE_KEY) != null) {
        // when writing out data, we should not do projection pushdown
        projectedSchema = tableSchema;
    } else {
        configuration.setBoolean(InputFormatConfig.CASE_SENSITIVE, false);
        String[] selectedColumns = ColumnProjectionUtils.getReadColumnNames(configuration);
        // When same table is joined multiple times, it is possible some selected columns are duplicated,
        // in this case wrong recordStructField position leads wrong value or ArrayIndexOutOfBoundException
        String[] distinctSelectedColumns = Arrays.stream(selectedColumns).distinct().toArray(String[]::new);
        projectedSchema = distinctSelectedColumns.length > 0 ? tableSchema.caseInsensitiveSelect(distinctSelectedColumns) : tableSchema;
        // or we cannot find selectOperator's column from inspector
        if (projectedSchema.columns().size() != distinctSelectedColumns.length) {
            projectedSchema = tableSchema;
        }
    }
    try {
        this.inspector = IcebergObjectInspector.create(projectedSchema);
    } catch (Exception e) {
        throw new SerDeException(e);
    }
}
Also used : PartitionField(org.apache.iceberg.PartitionField) Table(org.apache.iceberg.Table) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) Schema(org.apache.iceberg.Schema) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) PartitionSpec(org.apache.iceberg.PartitionSpec) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Aggregations

NoSuchTableException (org.apache.iceberg.exceptions.NoSuchTableException)12 Table (org.apache.iceberg.Table)6 IcebergUtil.getHadoopIcebergTable (com.facebook.presto.iceberg.IcebergUtil.getHadoopIcebergTable)3 SystemTable (com.facebook.presto.spi.SystemTable)3 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)3 TableIdentifier (org.apache.iceberg.catalog.TableIdentifier)3 TException (org.apache.thrift.TException)3 IcebergPrestoModelConverters.toIcebergTableIdentifier (com.facebook.presto.iceberg.util.IcebergPrestoModelConverters.toIcebergTableIdentifier)2 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)2 Table (org.apache.hadoop.hive.metastore.api.Table)2 PartitionSpec (org.apache.iceberg.PartitionSpec)2 Schema (org.apache.iceberg.Schema)2 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)1 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)1 PrestoException (com.facebook.presto.spi.PrestoException)1 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)1 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)1 DirectSqlTable (com.netflix.metacat.connector.hive.sql.DirectSqlTable)1 IOException (java.io.IOException)1 List (java.util.List)1