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