use of io.trino.spi.connector.SystemTable in project trino by trinodb.
the class BigQueryMetadata method getViewDefinitionSystemTable.
private Optional<SystemTable> getViewDefinitionSystemTable(ConnectorSession session, SchemaTableName viewDefinitionTableName, SchemaTableName sourceTableName) {
BigQueryClient client = bigQueryClientFactory.create(session);
String projectId = getProjectId(client);
String remoteSchemaName = client.toRemoteDataset(projectId, sourceTableName.getSchemaName()).map(RemoteDatabaseObject::getOnlyRemoteName).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
String remoteTableName = client.toRemoteTable(projectId, remoteSchemaName, sourceTableName.getTableName()).map(RemoteDatabaseObject::getOnlyRemoteName).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
TableInfo tableInfo = client.getTable(TableId.of(projectId, remoteSchemaName, remoteTableName)).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
if (!(tableInfo.getDefinition() instanceof ViewDefinition)) {
throw new TableNotFoundException(viewDefinitionTableName);
}
List<ColumnMetadata> columns = ImmutableList.of(new ColumnMetadata("query", VarcharType.VARCHAR));
List<Type> types = columns.stream().map(ColumnMetadata::getType).collect(toImmutableList());
Optional<String> query = Optional.ofNullable(((ViewDefinition) tableInfo.getDefinition()).getQuery());
Iterable<List<Object>> propertyValues = ImmutableList.of(ImmutableList.of(query.orElse("NULL")));
return Optional.of(createSystemTable(new ConnectorTableMetadata(sourceTableName, columns), constraint -> new InMemoryRecordSet(types, propertyValues).cursor()));
}
use of io.trino.spi.connector.SystemTable in project trino by trinodb.
the class IcebergMetadata method getRawSystemTable.
private Optional<SystemTable> getRawSystemTable(ConnectorSession session, SchemaTableName tableName) {
IcebergTableName name = IcebergTableName.from(tableName.getTableName());
if (name.getTableType() == DATA) {
return Optional.empty();
}
// load the base table for the system table
Table table;
try {
table = catalog.loadTable(session, new SchemaTableName(tableName.getSchemaName(), name.getTableName()));
} catch (TableNotFoundException e) {
return Optional.empty();
}
SchemaTableName systemTableName = new SchemaTableName(tableName.getSchemaName(), name.getTableNameWithType());
switch(name.getTableType()) {
case DATA:
// Handled above.
break;
case HISTORY:
if (name.getSnapshotId().isPresent()) {
throw new TrinoException(NOT_SUPPORTED, "Snapshot ID not supported for history table: " + systemTableName);
}
return Optional.of(new HistoryTable(systemTableName, table));
case SNAPSHOTS:
if (name.getSnapshotId().isPresent()) {
throw new TrinoException(NOT_SUPPORTED, "Snapshot ID not supported for snapshots table: " + systemTableName);
}
return Optional.of(new SnapshotsTable(systemTableName, typeManager, table));
case PARTITIONS:
return Optional.of(new PartitionTable(systemTableName, typeManager, table, getSnapshotId(table, name.getSnapshotId())));
case MANIFESTS:
return Optional.of(new ManifestsTable(systemTableName, table, getSnapshotId(table, name.getSnapshotId())));
case FILES:
return Optional.of(new FilesTable(systemTableName, typeManager, table, getSnapshotId(table, name.getSnapshotId())));
case PROPERTIES:
return Optional.of(new PropertiesTable(systemTableName, table));
}
return Optional.empty();
}
use of io.trino.spi.connector.SystemTable in project trino by trinodb.
the class CoordinatorSystemTablesProvider method getSystemTable.
@Override
public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTableName tableName) {
Optional<SystemTable> staticSystemTable = staticProvider.getSystemTable(session, tableName);
if (staticSystemTable.isPresent()) {
return staticSystemTable;
}
if (!isCoordinatorTransaction(session)) {
// this is a session from another coordinator, so there are no dynamic tables here for that session
return Optional.empty();
}
Optional<SystemTable> systemTable = metadata.getSystemTable(((FullConnectorSession) session).getSession(), new QualifiedObjectName(catalogName, tableName.getSchemaName(), tableName.getTableName()));
// dynamic tables require access to the transaction and thus can only run on the current coordinator
if (systemTable.isPresent() && systemTable.get().getDistribution() != SINGLE_COORDINATOR) {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Distribution for dynamic system table must be " + SINGLE_COORDINATOR);
}
return systemTable;
}
use of io.trino.spi.connector.SystemTable in project trino by trinodb.
the class PartitionsSystemTableProvider method getSystemTable.
@Override
public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSession session, SchemaTableName tableName) {
if (!PARTITIONS.matches(tableName)) {
return Optional.empty();
}
SchemaTableName sourceTableName = PARTITIONS.getSourceTableName(tableName);
Table sourceTable = metadata.getMetastore().getTable(sourceTableName.getSchemaName(), sourceTableName.getTableName()).orElse(null);
if (sourceTable == null || isDeltaLakeTable(sourceTable) || isIcebergTable(sourceTable)) {
return Optional.empty();
}
verifyOnline(sourceTableName, Optional.empty(), getProtectMode(sourceTable), sourceTable.getParameters());
HiveTableHandle sourceTableHandle = new HiveTableHandle(sourceTableName.getSchemaName(), sourceTableName.getTableName(), sourceTable.getParameters(), getPartitionKeyColumnHandles(sourceTable, typeManager), getRegularColumnHandles(sourceTable, typeManager, getTimestampPrecision(session)), getHiveBucketHandle(session, sourceTable, typeManager));
List<HiveColumnHandle> partitionColumns = sourceTableHandle.getPartitionColumns();
if (partitionColumns.isEmpty()) {
return Optional.empty();
}
List<Type> partitionColumnTypes = partitionColumns.stream().map(HiveColumnHandle::getType).collect(toImmutableList());
List<ColumnMetadata> partitionSystemTableColumns = partitionColumns.stream().map(column -> ColumnMetadata.builder().setName(column.getName()).setType(column.getType()).setComment(column.getComment()).setHidden(column.isHidden()).build()).collect(toImmutableList());
Map<Integer, HiveColumnHandle> fieldIdToColumnHandle = IntStream.range(0, partitionColumns.size()).boxed().collect(toImmutableMap(identity(), partitionColumns::get));
return Optional.of(createSystemTable(new ConnectorTableMetadata(tableName, partitionSystemTableColumns), constraint -> {
Constraint targetConstraint = new Constraint(constraint.transformKeys(fieldIdToColumnHandle::get));
Iterable<List<Object>> records = () -> stream(partitionManager.getPartitions(metadata.getMetastore(), sourceTableHandle, targetConstraint).getPartitions()).map(hivePartition -> IntStream.range(0, partitionColumns.size()).mapToObj(fieldIdToColumnHandle::get).map(columnHandle -> hivePartition.getKeys().get(columnHandle).getValue()).collect(// nullable
toList())).iterator();
return new InMemoryRecordSet(partitionColumnTypes, records).cursor();
}));
}
use of io.trino.spi.connector.SystemTable in project trino by trinodb.
the class IndexedTpchConnectorFactory method create.
@Override
public Connector create(String catalogName, Map<String, String> properties, ConnectorContext context) {
int splitsPerNode = getSplitsPerNode(properties);
TpchIndexedData indexedData = new TpchIndexedData(indexSpec);
NodeManager nodeManager = context.getNodeManager();
return new Connector() {
@Override
public ConnectorTransactionHandle beginTransaction(IsolationLevel isolationLevel, boolean readOnly, boolean autoCommit) {
return TpchTransactionHandle.INSTANCE;
}
@Override
public ConnectorMetadata getMetadata(ConnectorSession session, ConnectorTransactionHandle transactionHandle) {
return new TpchIndexMetadata(indexedData);
}
@Override
public ConnectorSplitManager getSplitManager() {
return new TpchSplitManager(nodeManager, splitsPerNode);
}
@Override
public ConnectorRecordSetProvider getRecordSetProvider() {
return new TpchRecordSetProvider(DecimalTypeMapping.DOUBLE);
}
@Override
public ConnectorIndexProvider getIndexProvider() {
return new TpchIndexProvider(indexedData);
}
@Override
public Set<SystemTable> getSystemTables() {
return ImmutableSet.of(new ExampleSystemTable());
}
@Override
public ConnectorNodePartitioningProvider getNodePartitioningProvider() {
return new TpchNodePartitioningProvider(nodeManager, splitsPerNode);
}
};
}
Aggregations