Search in sources :

Example 1 with ViewAlreadyExistsException

use of io.trino.plugin.hive.ViewAlreadyExistsException 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)

Aggregations

TableAlreadyExistsException (io.trino.plugin.hive.TableAlreadyExistsException)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 BaseTable (org.apache.iceberg.BaseTable)1 Table (org.apache.iceberg.Table)1