Search in sources :

Example 1 with TableAlreadyExistsException

use of io.trino.plugin.hive.TableAlreadyExistsException in project trino by trinodb.

the class SemiTransactionalHiveMetastore method createTable.

/**
 * {@code currentLocation} needs to be supplied if a writePath exists for the table.
 */
public synchronized void createTable(ConnectorSession session, Table table, PrincipalPrivileges principalPrivileges, Optional<Path> currentPath, Optional<List<String>> files, boolean ignoreExisting, PartitionStatistics statistics, boolean cleanExtraOutputFilesOnCommit) {
    setShared();
    // When creating a table, it should never have partition actions. This is just a sanity check.
    checkNoPartitionAction(table.getDatabaseName(), table.getTableName());
    Action<TableAndMore> oldTableAction = tableActions.get(table.getSchemaTableName());
    TableAndMore tableAndMore = new TableAndMore(table, Optional.of(principalPrivileges), currentPath, files, ignoreExisting, statistics, statistics, cleanExtraOutputFilesOnCommit);
    if (oldTableAction == null) {
        HdfsContext hdfsContext = new HdfsContext(session);
        tableActions.put(table.getSchemaTableName(), new Action<>(ActionType.ADD, tableAndMore, hdfsContext, session.getQueryId()));
        return;
    }
    switch(oldTableAction.getType()) {
        case DROP:
            if (!oldTableAction.getHdfsContext().getIdentity().getUser().equals(session.getUser())) {
                throw new TrinoException(TRANSACTION_CONFLICT, "Operation on the same table with different user in the same transaction is not supported");
            }
            HdfsContext hdfsContext = new HdfsContext(session);
            tableActions.put(table.getSchemaTableName(), new Action<>(ActionType.ALTER, tableAndMore, hdfsContext, session.getQueryId()));
            return;
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
        case DELETE_ROWS:
        case UPDATE:
            throw new TableAlreadyExistsException(table.getSchemaTableName());
        case DROP_PRESERVE_DATA:
            // TODO
            break;
    }
    throw new IllegalStateException("Unknown action type");
}
Also used : TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TrinoException(io.trino.spi.TrinoException) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext)

Example 2 with TableAlreadyExistsException

use of io.trino.plugin.hive.TableAlreadyExistsException in project trino by trinodb.

the class GlueHiveMetastore method createTable.

@Override
public void createTable(Table table, PrincipalPrivileges principalPrivileges) {
    try {
        TableInput input = GlueInputConverter.convertTable(table);
        stats.getCreateTable().call(() -> glueClient.createTable(new CreateTableRequest().withCatalogId(catalogId).withDatabaseName(table.getDatabaseName()).withTableInput(input)));
    } catch (AlreadyExistsException e) {
        throw new TableAlreadyExistsException(new SchemaTableName(table.getDatabaseName(), table.getTableName()));
    } catch (EntityNotFoundException e) {
        throw new SchemaNotFoundException(table.getDatabaseName());
    } catch (AmazonServiceException e) {
        throw new TrinoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : TableInput(com.amazonaws.services.glue.model.TableInput) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) AlreadyExistsException(com.amazonaws.services.glue.model.AlreadyExistsException) SchemaAlreadyExistsException(io.trino.plugin.hive.SchemaAlreadyExistsException) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) AmazonServiceException(com.amazonaws.AmazonServiceException) TrinoException(io.trino.spi.TrinoException) EntityNotFoundException(com.amazonaws.services.glue.model.EntityNotFoundException) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) CreateTableRequest(com.amazonaws.services.glue.model.CreateTableRequest) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 3 with TableAlreadyExistsException

use of io.trino.plugin.hive.TableAlreadyExistsException in project trino by trinodb.

the class TrinoHiveCatalog method createView.

@Override
public void createView(ConnectorSession session, SchemaTableName schemaViewName, ConnectorViewDefinition definition, boolean replace) {
    if (isUsingSystemSecurity) {
        definition = definition.withoutOwner();
    }
    Map<String, String> properties = ImmutableMap.<String, String>builder().put(PRESTO_VIEW_FLAG, "true").put(TRINO_CREATED_BY, TRINO_CREATED_BY_VALUE).put(PRESTO_VERSION_NAME, trinoVersion).put(PRESTO_QUERY_ID_NAME, session.getQueryId()).put(TABLE_COMMENT, PRESTO_VIEW_COMMENT).buildOrThrow();
    io.trino.plugin.hive.metastore.Table.Builder tableBuilder = io.trino.plugin.hive.metastore.Table.builder().setDatabaseName(schemaViewName.getSchemaName()).setTableName(schemaViewName.getTableName()).setOwner(isUsingSystemSecurity ? Optional.empty() : Optional.of(session.getUser())).setTableType(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW.name()).setDataColumns(ImmutableList.of(new Column("dummy", HIVE_STRING, Optional.empty()))).setPartitionColumns(ImmutableList.of()).setParameters(properties).setViewOriginalText(Optional.of(encodeViewData(definition))).setViewExpandedText(Optional.of(PRESTO_VIEW_EXPANDED_TEXT_MARKER));
    tableBuilder.getStorageBuilder().setStorageFormat(VIEW_STORAGE_FORMAT).setLocation("");
    io.trino.plugin.hive.metastore.Table table = tableBuilder.build();
    PrincipalPrivileges principalPrivileges = isUsingSystemSecurity ? NO_PRIVILEGES : buildInitialPrivilegeSet(session.getUser());
    Optional<io.trino.plugin.hive.metastore.Table> existing = metastore.getTable(schemaViewName.getSchemaName(), schemaViewName.getTableName());
    if (existing.isPresent()) {
        if (!replace || !isPrestoView(existing.get())) {
            throw new ViewAlreadyExistsException(schemaViewName);
        }
        metastore.replaceTable(schemaViewName.getSchemaName(), schemaViewName.getTableName(), table, principalPrivileges);
        return;
    }
    try {
        metastore.createTable(table, principalPrivileges);
    } catch (TableAlreadyExistsException e) {
        throw new ViewAlreadyExistsException(e.getTableName());
    }
}
Also used : TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) BaseTable(org.apache.iceberg.BaseTable) Table(org.apache.iceberg.Table) IcebergUtil.loadIcebergTable(io.trino.plugin.iceberg.IcebergUtil.loadIcebergTable) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) Column(io.trino.plugin.hive.metastore.Column) ViewAlreadyExistsException(io.trino.plugin.hive.ViewAlreadyExistsException)

Example 4 with TableAlreadyExistsException

use of io.trino.plugin.hive.TableAlreadyExistsException in project trino by trinodb.

the class InMemoryThriftMetastore method alterTable.

@Override
public synchronized void alterTable(HiveIdentity identity, String databaseName, String tableName, Table newTable) {
    SchemaTableName oldName = new SchemaTableName(databaseName, tableName);
    SchemaTableName newName = new SchemaTableName(newTable.getDbName(), newTable.getTableName());
    // if the name did not change, this is a simple schema change
    if (oldName.equals(newName)) {
        if (relations.replace(oldName, newTable) == null) {
            throw new TableNotFoundException(oldName);
        }
        return;
    }
    // remove old table definition and add the new one
    Table table = relations.get(oldName);
    if (table == null) {
        throw new TableNotFoundException(oldName);
    }
    if (relations.putIfAbsent(newName, newTable) != null) {
        throw new TableAlreadyExistsException(newName);
    }
    relations.remove(oldName);
}
Also used : TableNotFoundException(io.trino.spi.connector.TableNotFoundException) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) Table(org.apache.hadoop.hive.metastore.api.Table) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 5 with TableAlreadyExistsException

use of io.trino.plugin.hive.TableAlreadyExistsException in project trino by trinodb.

the class InMemoryThriftMetastore method createTable.

@Override
public synchronized void createTable(HiveIdentity identity, Table table) {
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
    if (tableType == VIRTUAL_VIEW) {
        checkArgument(table.getSd().getLocation() == null, "Storage location for view must be null");
    } else {
        File directory = new File(new Path(table.getSd().getLocation()).toUri());
        checkArgument(directory.exists(), "Table directory does not exist");
        if (tableType == MANAGED_TABLE) {
            checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
        }
    }
    SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName());
    Table tableCopy = table.deepCopy();
    if (relations.putIfAbsent(schemaTableName, tableCopy) != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    if (tableType == VIRTUAL_VIEW) {
        views.put(schemaTableName, tableCopy);
    }
    PrincipalPrivilegeSet privileges = table.getPrivileges();
    if (privileges != null) {
        throw new UnsupportedOperationException();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Table(org.apache.hadoop.hive.metastore.api.Table) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) File(java.io.File) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Aggregations

TableAlreadyExistsException (io.trino.plugin.hive.TableAlreadyExistsException)5 SchemaTableName (io.trino.spi.connector.SchemaTableName)3 TrinoException (io.trino.spi.TrinoException)2 Table (org.apache.hadoop.hive.metastore.api.Table)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AlreadyExistsException (com.amazonaws.services.glue.model.AlreadyExistsException)1 CreateTableRequest (com.amazonaws.services.glue.model.CreateTableRequest)1 EntityNotFoundException (com.amazonaws.services.glue.model.EntityNotFoundException)1 TableInput (com.amazonaws.services.glue.model.TableInput)1 HdfsContext (io.trino.plugin.hive.HdfsEnvironment.HdfsContext)1 SchemaAlreadyExistsException (io.trino.plugin.hive.SchemaAlreadyExistsException)1 ViewAlreadyExistsException (io.trino.plugin.hive.ViewAlreadyExistsException)1 Column (io.trino.plugin.hive.metastore.Column)1 PrincipalPrivileges (io.trino.plugin.hive.metastore.PrincipalPrivileges)1 IcebergUtil.loadIcebergTable (io.trino.plugin.iceberg.IcebergUtil.loadIcebergTable)1 SchemaNotFoundException (io.trino.spi.connector.SchemaNotFoundException)1 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)1 File (java.io.File)1 Path (org.apache.hadoop.fs.Path)1 TableType (org.apache.hadoop.hive.metastore.TableType)1