use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class PostgreSqlClient method getColumns.
@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
if (tableHandle.getColumns().isPresent()) {
return tableHandle.getColumns().get();
}
checkArgument(tableHandle.isNamedRelation(), "Cannot get columns for %s", tableHandle);
SchemaTableName schemaTableName = tableHandle.getRequiredNamedRelation().getSchemaTableName();
try (Connection connection = connectionFactory.openConnection(session)) {
Map<String, Integer> arrayColumnDimensions = ImmutableMap.of();
if (getArrayMapping(session) == AS_ARRAY) {
arrayColumnDimensions = getArrayColumnDimensions(connection, tableHandle);
}
try (ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) {
int allColumns = 0;
List<JdbcColumnHandle> columns = new ArrayList<>();
while (resultSet.next()) {
allColumns++;
String columnName = resultSet.getString("COLUMN_NAME");
JdbcTypeHandle typeHandle = new JdbcTypeHandle(getInteger(resultSet, "DATA_TYPE").orElseThrow(() -> new IllegalStateException("DATA_TYPE is null")), Optional.of(resultSet.getString("TYPE_NAME")), getInteger(resultSet, "COLUMN_SIZE"), getInteger(resultSet, "DECIMAL_DIGITS"), Optional.ofNullable(arrayColumnDimensions.get(columnName)), Optional.empty());
Optional<ColumnMapping> columnMapping = toColumnMapping(session, connection, typeHandle);
log.debug("Mapping data type of '%s' column '%s': %s mapped to %s", schemaTableName, columnName, typeHandle, columnMapping);
// skip unsupported column types
if (columnMapping.isPresent()) {
boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
Optional<String> comment = Optional.ofNullable(resultSet.getString("REMARKS"));
columns.add(JdbcColumnHandle.builder().setColumnName(columnName).setJdbcTypeHandle(typeHandle).setColumnType(columnMapping.get().getType()).setNullable(nullable).setComment(comment).build());
}
if (columnMapping.isEmpty()) {
UnsupportedTypeHandling unsupportedTypeHandling = getUnsupportedTypeHandling(session);
verify(unsupportedTypeHandling == IGNORE, "Unsupported type handling is set to %s, but toColumnMapping() returned empty for %s", unsupportedTypeHandling, typeHandle);
}
}
if (columns.isEmpty()) {
// A table may have no supported columns. In rare cases a table might have no columns at all.
throw new TableNotFoundException(tableHandle.getSchemaTableName(), format("Table '%s' has no supported columns (all %s columns are not supported)", tableHandle.getSchemaTableName(), allColumns));
}
return ImmutableList.copyOf(columns);
}
} catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class RedisMetadata method getTableMetadata.
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) {
SchemaTableName schemaTableName = ((RedisTableHandle) tableHandle).toSchemaTableName();
ConnectorTableMetadata tableMetadata = getTableMetadata(schemaTableName);
if (tableMetadata == null) {
throw new TableNotFoundException(schemaTableName);
}
return tableMetadata;
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class BigQuerySplitManager method createEmptyProjection.
private List<BigQuerySplit> createEmptyProjection(ConnectorSession session, TableId remoteTableId, int actualParallelism, Optional<String> filter) {
BigQueryClient client = bigQueryClientFactory.create(session);
log.debug("createEmptyProjection(tableId=%s, actualParallelism=%s, filter=[%s])", remoteTableId, actualParallelism, filter);
try {
long numberOfRows;
if (filter.isPresent()) {
// count the rows based on the filter
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
} else {
// no filters, so we can take the value from the table info when the object is TABLE
TableInfo tableInfo = client.getTable(remoteTableId).orElseThrow(() -> new TableNotFoundException(new SchemaTableName(remoteTableId.getDataset(), remoteTableId.getTable())));
if (tableInfo.getDefinition().getType() == TABLE) {
numberOfRows = tableInfo.getNumRows().longValue();
} else if (tableInfo.getDefinition().getType() == VIEW) {
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
} else {
throw new TrinoException(NOT_SUPPORTED, "Unsupported table type: " + tableInfo.getDefinition().getType());
}
}
long rowsPerSplit = numberOfRows / actualParallelism;
// need to be added to one fo the split due to integer division
long remainingRows = numberOfRows - (rowsPerSplit * actualParallelism);
List<BigQuerySplit> splits = range(0, actualParallelism).mapToObj(ignored -> BigQuerySplit.emptyProjection(rowsPerSplit)).collect(toList());
splits.set(0, BigQuerySplit.emptyProjection(rowsPerSplit + remainingRows));
return splits;
} catch (BigQueryException e) {
throw new TrinoException(BIGQUERY_FAILED_TO_EXECUTE_QUERY, "Failed to compute empty projection", e);
}
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class BigQueryClient method getColumns.
public List<BigQueryColumnHandle> getColumns(BigQueryTableHandle tableHandle) {
TableInfo tableInfo = getTable(tableHandle.getRemoteTableName().toTableId()).orElseThrow(() -> new TableNotFoundException(tableHandle.getSchemaTableName()));
Schema schema = tableInfo.getDefinition().getSchema();
if (schema == null) {
throw new TableNotFoundException(tableHandle.getSchemaTableName(), format("Table '%s' has no schema", tableHandle.getSchemaTableName()));
}
return schema.getFields().stream().filter(Conversions::isSupportedType).map(Conversions::toColumnHandle).collect(toImmutableList());
}
use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.
the class ReadSessionCreator method create.
public ReadSession create(ConnectorSession session, TableId remoteTable, List<String> selectedFields, Optional<String> filter, int parallelism) {
BigQueryClient client = bigQueryClientFactory.create(session);
TableInfo tableDetails = client.getTable(remoteTable).orElseThrow(() -> new TableNotFoundException(new SchemaTableName(remoteTable.getDataset(), remoteTable.getTable())));
TableInfo actualTable = getActualTable(client, tableDetails, selectedFields);
List<String> filteredSelectedFields = selectedFields.stream().filter(BigQueryUtil::validColumnName).collect(toList());
try (BigQueryReadClient bigQueryReadClient = bigQueryReadClientFactory.create(session)) {
ReadSession.TableReadOptions.Builder readOptions = ReadSession.TableReadOptions.newBuilder().addAllSelectedFields(filteredSelectedFields);
filter.ifPresent(readOptions::setRowRestriction);
ReadSession readSession = bigQueryReadClient.createReadSession(CreateReadSessionRequest.newBuilder().setParent("projects/" + client.getProjectId()).setReadSession(ReadSession.newBuilder().setDataFormat(DataFormat.AVRO).setTable(toTableResourceName(actualTable.getTableId())).setReadOptions(readOptions)).setMaxStreamCount(parallelism).build());
return readSession;
}
}
Aggregations