use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class CassandraSession method getTable.
public CassandraTable getTable(SchemaTableName schemaTableName) throws TableNotFoundException {
KeyspaceMetadata keyspace = getKeyspaceByCaseInsensitiveName(schemaTableName.getSchemaName());
AbstractTableMetadata tableMeta = getTableMetadata(keyspace, schemaTableName.getTableName());
List<String> columnNames = new ArrayList<>();
List<ColumnMetadata> columns = tableMeta.getColumns();
checkColumnNames(columns);
for (ColumnMetadata columnMetadata : columns) {
columnNames.add(columnMetadata.getName());
}
// check if there is a comment to establish column ordering
String comment = tableMeta.getOptions().getComment();
Set<String> hiddenColumns = ImmutableSet.of();
if (comment != null && comment.startsWith(PRESTO_COMMENT_METADATA)) {
String columnOrderingString = comment.substring(PRESTO_COMMENT_METADATA.length());
// column ordering
List<ExtraColumnMetadata> extras = extraColumnMetadataCodec.fromJson(columnOrderingString);
List<String> explicitColumnOrder = new ArrayList<>(ImmutableList.copyOf(transform(extras, ExtraColumnMetadata::getName)));
hiddenColumns = extras.stream().filter(ExtraColumnMetadata::isHidden).map(ExtraColumnMetadata::getName).collect(toImmutableSet());
// add columns not in the comment to the ordering
List<String> remaining = columnNames.stream().filter(name -> !explicitColumnOrder.contains(name)).collect(toList());
explicitColumnOrder.addAll(remaining);
// sort the actual columns names using the explicit column order (this allows for missing columns)
columnNames = Ordering.explicit(explicitColumnOrder).sortedCopy(columnNames);
}
ImmutableList.Builder<CassandraColumnHandle> columnHandles = ImmutableList.builder();
// add primary keys first
Set<String> primaryKeySet = new HashSet<>();
for (ColumnMetadata columnMeta : tableMeta.getPartitionKey()) {
primaryKeySet.add(columnMeta.getName());
boolean hidden = hiddenColumns.contains(columnMeta.getName());
CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, true, false, columnNames.indexOf(columnMeta.getName()), hidden).orElseThrow(() -> new TrinoException(NOT_SUPPORTED, "Unsupported partition key type: " + columnMeta.getType().getName()));
columnHandles.add(columnHandle);
}
// add clustering columns
for (ColumnMetadata columnMeta : tableMeta.getClusteringColumns()) {
primaryKeySet.add(columnMeta.getName());
boolean hidden = hiddenColumns.contains(columnMeta.getName());
Optional<CassandraColumnHandle> columnHandle = buildColumnHandle(tableMeta, columnMeta, false, true, columnNames.indexOf(columnMeta.getName()), hidden);
columnHandle.ifPresent(columnHandles::add);
}
// add other columns
for (ColumnMetadata columnMeta : columns) {
if (!primaryKeySet.contains(columnMeta.getName())) {
boolean hidden = hiddenColumns.contains(columnMeta.getName());
Optional<CassandraColumnHandle> columnHandle = buildColumnHandle(tableMeta, columnMeta, false, false, columnNames.indexOf(columnMeta.getName()), hidden);
columnHandle.ifPresent(columnHandles::add);
}
}
List<CassandraColumnHandle> sortedColumnHandles = columnHandles.build().stream().sorted(comparing(CassandraColumnHandle::getOrdinalPosition)).collect(toList());
CassandraTableHandle tableHandle = new CassandraTableHandle(tableMeta.getKeyspace().getName(), tableMeta.getName());
return new CassandraTable(tableHandle, sortedColumnHandles);
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class ViewReaderUtil method coralTableRedirectionResolver.
private static CoralTableRedirectionResolver coralTableRedirectionResolver(ConnectorSession session, BiFunction<ConnectorSession, SchemaTableName, Optional<CatalogSchemaTableName>> tableRedirectionResolver, MetadataProvider metadataProvider) {
return schemaTableName -> tableRedirectionResolver.apply(session, schemaTableName).map(target -> {
ConnectorTableSchema tableSchema = metadataProvider.getRelationMetadata(session, target).orElseThrow(() -> new TableNotFoundException(target.getSchemaTableName(), format("%s is redirected to %s, but that relation cannot be found", schemaTableName, target)));
List<Column> columns = tableSchema.getColumns().stream().filter(columnSchema -> !columnSchema.isHidden()).map(columnSchema -> new Column(columnSchema.getName(), toHiveType(columnSchema.getType()), Optional.empty())).collect(toImmutableList());
Table table = Table.builder().setDatabaseName(schemaTableName.getSchemaName()).setTableName(schemaTableName.getTableName()).setTableType(EXTERNAL_TABLE.name()).setDataColumns(columns).withStorage(storage -> storage.setStorageFormat(fromHiveStorageFormat(TEXTFILE))).setOwner(Optional.empty()).build();
return toMetastoreApiTable(table);
});
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class RegisterPartitionProcedure method doRegisterPartition.
private void doRegisterPartition(ConnectorSession session, ConnectorAccessControl accessControl, String schemaName, String tableName, List<String> partitionColumn, List<String> partitionValues, String location) {
if (!allowRegisterPartition) {
throw new TrinoException(PERMISSION_DENIED, "register_partition procedure is disabled");
}
SemiTransactionalHiveMetastore metastore = hiveMetadataFactory.create(session.getIdentity(), true).getMetastore();
HdfsContext hdfsContext = new HdfsContext(session);
SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
Table table = metastore.getTable(schemaName, tableName).orElseThrow(() -> new TableNotFoundException(schemaTableName));
accessControl.checkCanInsertIntoTable(null, schemaTableName);
checkIsPartitionedTable(table);
checkPartitionColumns(table, partitionColumn);
Optional<Partition> partition = metastore.unsafeGetRawHiveMetastoreClosure().getPartition(schemaName, tableName, partitionValues);
if (partition.isPresent()) {
String partitionName = FileUtils.makePartName(partitionColumn, partitionValues);
throw new TrinoException(ALREADY_EXISTS, format("Partition [%s] is already registered with location %s", partitionName, partition.get().getStorage().getLocation()));
}
Path partitionLocation;
if (location == null) {
partitionLocation = new Path(table.getStorage().getLocation(), FileUtils.makePartName(partitionColumn, partitionValues));
} else {
partitionLocation = new Path(location);
}
if (!HiveWriteUtils.pathExists(hdfsContext, hdfsEnvironment, partitionLocation)) {
throw new TrinoException(INVALID_PROCEDURE_ARGUMENT, "Partition location does not exist: " + partitionLocation);
}
metastore.addPartition(session, table.getDatabaseName(), table.getTableName(), buildPartitionObject(session, table, partitionValues, partitionLocation), partitionLocation, // no need for failed attempts cleanup
Optional.empty(), PartitionStatistics.empty(), false);
metastore.commit();
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class SqlStandardAccessControlMetadata method listTablePrivileges.
@Override
public List<GrantInfo> listTablePrivileges(ConnectorSession session, List<SchemaTableName> tableNames) {
Set<HivePrincipal> principals = ThriftMetastoreUtil.listEnabledPrincipals(session.getIdentity(), metastore::listRoleGrants).collect(toImmutableSet());
boolean isAdminRoleSet = hasAdminRole(principals);
ImmutableList.Builder<GrantInfo> result = ImmutableList.builder();
for (SchemaTableName tableName : tableNames) {
try {
result.addAll(buildGrants(principals, isAdminRoleSet, tableName));
} catch (TableNotFoundException e) {
// table disappeared during listing operation
} catch (HiveViewNotSupportedException e) {
// table is an unsupported hive view but shouldn't fail listTablePrivileges.
}
}
return result.build();
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class DefaultJdbcMetadata method listTableColumns.
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix) {
ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();
List<SchemaTableName> tables = prefix.toOptionalSchemaTableName().<List<SchemaTableName>>map(ImmutableList::of).orElseGet(() -> listTables(session, prefix.getSchema()));
for (SchemaTableName tableName : tables) {
try {
jdbcClient.getTableHandle(session, tableName).ifPresent(tableHandle -> columns.put(tableName, getTableMetadata(session, tableHandle).getColumns()));
} catch (TableNotFoundException | AccessDeniedException e) {
// table disappeared during listing operation or user is not allowed to access it
// these exceptions are ignored because listTableColumns is used for metadata queries (SELECT FROM information_schema)
}
}
return columns.buildOrThrow();
}
Aggregations