Search in sources :

Example 11 with Column

use of bio.terra.common.Column in project jade-data-repo by DataBiosphere.

the class DatasetDaoTest method primaryKeyTest.

@Test
public void primaryKeyTest() throws IOException, SQLException {
    UUID datasetId = createDataset("dataset-primary-key.json");
    try {
        Dataset fromDB = datasetDao.retrieve(datasetId);
        DatasetTable variants = fromDB.getTableByName("variant").orElseThrow(IllegalStateException::new);
        DatasetTable freqAnalysis = fromDB.getTableByName("frequency_analysis").orElseThrow(IllegalStateException::new);
        DatasetTable metaAnalysis = fromDB.getTableByName("meta_analysis").orElseThrow(IllegalStateException::new);
        assertThat("single-column primary keys are set correctly", variants.getPrimaryKey().stream().map(Column::getName).collect(Collectors.toList()), equalTo(Collections.singletonList("id")));
        assertThat("dual-column primary keys are set correctly", metaAnalysis.getPrimaryKey().stream().map(Column::getName).collect(Collectors.toList()), equalTo(Arrays.asList("variant_id", "phenotype")));
        assertThat("many-column primary keys are set correctly", freqAnalysis.getPrimaryKey().stream().map(Column::getName).collect(Collectors.toList()), equalTo(Arrays.asList("variant_id", "ancestry", "phenotype")));
    } finally {
        datasetDao.delete(datasetId);
    }
}
Also used : Column(bio.terra.common.Column) UUID(java.util.UUID) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with Column

use of bio.terra.common.Column in project jade-data-repo by DataBiosphere.

the class DatasetTableDao method retrieveTables.

public List<DatasetTable> retrieveTables(UUID parentId) {
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("dataset_id", parentId);
    return jdbcTemplate.query(sqlSelectTable, params, (rs, rowNum) -> {
        DatasetTable table = new DatasetTable().id(rs.getObject("id", UUID.class)).name(rs.getString("name")).rawTableName(rs.getString("raw_table_name")).softDeleteTableName(rs.getString("soft_delete_table_name"));
        List<Column> columns = retrieveColumns(table);
        table.columns(columns);
        Map<String, Column> columnMap = columns.stream().collect(Collectors.toMap(Column::getName, Function.identity()));
        List<String> primaryKey = DaoUtils.getStringList(rs, "primary_key");
        List<Column> naturalKeyColumns = primaryKey.stream().map(columnMap::get).collect(Collectors.toList());
        table.primaryKey(naturalKeyColumns);
        long bqPartitionVersion = rs.getLong("bigquery_partition_config_version");
        String bqPartitionConfig = rs.getString("bigquery_partition_config");
        if (bqPartitionVersion == 1) {
            try {
                table.bigQueryPartitionConfig(objectMapper.readValue(bqPartitionConfig, BigQueryPartitionConfigV1.class));
            } catch (Exception ex) {
                throw new CorruptMetadataException("Malformed BigQuery partition config", ex);
            }
        } else {
            throw new CorruptMetadataException("Unknown BigQuery partition config version: " + bqPartitionVersion);
        }
        return table;
    });
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Column(bio.terra.common.Column) CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) IOException(java.io.IOException) CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) SQLException(java.sql.SQLException)

Example 13 with Column

use of bio.terra.common.Column in project jade-data-repo by DataBiosphere.

the class DatasetTableDao method createColumns.

private void createColumns(UUID tableId, Collection<Column> columns) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("table_id", tableId);
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    for (Column column : columns) {
        params.addValue("name", column.getName());
        params.addValue("type", column.getType());
        params.addValue("array_of", column.isArrayOf());
        jdbcTemplate.update(sqlInsertColumn, params, keyHolder);
        UUID columnId = keyHolder.getId();
        column.id(columnId);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Column(bio.terra.common.Column) DaoKeyHolder(bio.terra.common.DaoKeyHolder) UUID(java.util.UUID)

Example 14 with Column

use of bio.terra.common.Column in project jade-data-repo by DataBiosphere.

the class SnapshotService method conjureSnapshotTablesFromRowIds.

private void conjureSnapshotTablesFromRowIds(SnapshotRequestRowIdModel requestRowIdModel, Snapshot snapshot, SnapshotSource snapshotSource) {
    // TODO this will need to be changed when we have more than one dataset per snapshot (>1 contentsModel)
    List<SnapshotTable> tableList = new ArrayList<>();
    snapshot.snapshotTables(tableList);
    List<SnapshotMapTable> mapTableList = new ArrayList<>();
    snapshotSource.snapshotMapTables(mapTableList);
    Dataset dataset = snapshotSource.getDataset();
    // create a lookup from tableName -> table spec from the request
    Map<String, SnapshotRequestRowIdTableModel> requestTableLookup = requestRowIdModel.getTables().stream().collect(Collectors.toMap(SnapshotRequestRowIdTableModel::getTableName, Function.identity()));
    // for each dataset table specified in the request, create a table in the snapshot with the same name
    for (DatasetTable datasetTable : dataset.getTables()) {
        if (!requestTableLookup.containsKey(datasetTable.getName())) {
            // only capture the dataset tables in the request model
            continue;
        }
        List<Column> columnList = new ArrayList<>();
        SnapshotTable snapshotTable = new SnapshotTable().name(datasetTable.getName()).columns(columnList);
        tableList.add(snapshotTable);
        List<SnapshotMapColumn> mapColumnList = new ArrayList<>();
        mapTableList.add(new SnapshotMapTable().fromTable(datasetTable).toTable(snapshotTable).snapshotMapColumns(mapColumnList));
        // for each dataset column specified in the request, create a column in the snapshot with the same name
        Set<String> requestColumns = new HashSet<>(requestTableLookup.get(datasetTable.getName()).getColumns());
        datasetTable.getColumns().stream().filter(c -> requestColumns.contains(c.getName())).forEach(datasetColumn -> {
            Column snapshotColumn = new Column().name(datasetColumn.getName());
            SnapshotMapColumn snapshotMapColumn = new SnapshotMapColumn().fromColumn(datasetColumn).toColumn(snapshotColumn);
            columnList.add(snapshotColumn);
            mapColumnList.add(snapshotMapColumn);
        });
    }
}
Also used : DatasetService(bio.terra.service.dataset.DatasetService) SnapshotDeleteFlight(bio.terra.service.snapshot.flight.delete.SnapshotDeleteFlight) SnapshotModel(bio.terra.model.SnapshotModel) FireStoreDependencyDao(bio.terra.service.filedata.google.firestore.FireStoreDependencyDao) Autowired(org.springframework.beans.factory.annotation.Autowired) DatasetTable(bio.terra.service.dataset.DatasetTable) RelationshipModel(bio.terra.model.RelationshipModel) Map(java.util.Map) Table(bio.terra.common.Table) JobMapKeys(bio.terra.service.job.JobMapKeys) SnapshotRequestQueryModel(bio.terra.model.SnapshotRequestQueryModel) DataLocationService(bio.terra.service.resourcemanagement.DataLocationService) ValidationException(bio.terra.app.controller.exception.ValidationException) Query(bio.terra.grammar.Query) AssetColumn(bio.terra.service.dataset.AssetColumn) SnapshotRequestRowIdTableModel(bio.terra.model.SnapshotRequestRowIdTableModel) Set(java.util.Set) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) UUID(java.util.UUID) SnapshotRequestContentsModel(bio.terra.model.SnapshotRequestContentsModel) Collectors(java.util.stream.Collectors) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) List(java.util.List) AssetSpecification(bio.terra.service.dataset.AssetSpecification) Optional(java.util.Optional) BigQueryPdao(bio.terra.service.tabulardata.google.BigQueryPdao) AuthenticatedUserRequest(bio.terra.service.iam.AuthenticatedUserRequest) SnapshotCreateFlight(bio.terra.service.snapshot.flight.create.SnapshotCreateFlight) JobService(bio.terra.service.job.JobService) Relationship(bio.terra.common.Relationship) HashMap(java.util.HashMap) Column(bio.terra.common.Column) SnapshotRequestAssetModel(bio.terra.model.SnapshotRequestAssetModel) TableModel(bio.terra.model.TableModel) AssetNotFoundException(bio.terra.service.snapshot.exception.AssetNotFoundException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) InvalidSnapshotException(bio.terra.service.snapshot.exception.InvalidSnapshotException) MetadataEnumeration(bio.terra.common.MetadataEnumeration) AssetTable(bio.terra.service.dataset.AssetTable) SnapshotSourceModel(bio.terra.model.SnapshotSourceModel) ColumnModel(bio.terra.model.ColumnModel) RelationshipTermModel(bio.terra.model.RelationshipTermModel) Component(org.springframework.stereotype.Component) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) SnapshotRequestRowIdModel(bio.terra.model.SnapshotRequestRowIdModel) Dataset(bio.terra.service.dataset.Dataset) Collections(java.util.Collections) Dataset(bio.terra.service.dataset.Dataset) ArrayList(java.util.ArrayList) SnapshotRequestRowIdTableModel(bio.terra.model.SnapshotRequestRowIdTableModel) AssetColumn(bio.terra.service.dataset.AssetColumn) Column(bio.terra.common.Column) DatasetTable(bio.terra.service.dataset.DatasetTable) HashSet(java.util.HashSet)

Example 15 with Column

use of bio.terra.common.Column in project jade-data-repo by DataBiosphere.

the class SnapshotService method conjureSnapshotTablesFromDatasetTables.

private void conjureSnapshotTablesFromDatasetTables(Snapshot snapshot, SnapshotSource snapshotSource) {
    // TODO this will need to be changed when we have more than one dataset per snapshot (>1 contentsModel)
    // for each dataset table specified in the request, create a table in the snapshot with the same name
    Dataset dataset = snapshotSource.getDataset();
    List<SnapshotTable> tableList = new ArrayList<>();
    List<SnapshotMapTable> mapTableList = new ArrayList<>();
    for (DatasetTable datasetTable : dataset.getTables()) {
        List<Column> columnList = new ArrayList<>();
        List<SnapshotMapColumn> mapColumnList = new ArrayList<>();
        // for each dataset column specified in the request, create a column in the snapshot with the same name
        for (Column datasetColumn : datasetTable.getColumns()) {
            Column snapshotColumn = new Column().name(datasetColumn.getName());
            SnapshotMapColumn snapshotMapColumn = new SnapshotMapColumn().fromColumn(datasetColumn).toColumn(snapshotColumn);
            columnList.add(snapshotColumn);
            mapColumnList.add(snapshotMapColumn);
        }
        // create snapshot tables & mapping with the proper dataset name and columns
        SnapshotTable snapshotTable = new SnapshotTable().name(datasetTable.getName()).columns(columnList);
        tableList.add(snapshotTable);
        mapTableList.add(new SnapshotMapTable().fromTable(datasetTable).toTable(snapshotTable).snapshotMapColumns(mapColumnList));
    }
    // set the snapshot tables and mapping
    snapshot.snapshotTables(tableList);
    snapshotSource.snapshotMapTables(mapTableList);
}
Also used : AssetColumn(bio.terra.service.dataset.AssetColumn) Column(bio.terra.common.Column) Dataset(bio.terra.service.dataset.Dataset) ArrayList(java.util.ArrayList) DatasetTable(bio.terra.service.dataset.DatasetTable)

Aggregations

Column (bio.terra.common.Column)18 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)8 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)6 Table (bio.terra.common.Table)5 Relationship (bio.terra.common.Relationship)4 AssetColumn (bio.terra.service.dataset.AssetColumn)4 AssetTable (bio.terra.service.dataset.AssetTable)4 Dataset (bio.terra.service.dataset.Dataset)4 DatasetTable (bio.terra.service.dataset.DatasetTable)4 SnapshotMapColumn (bio.terra.service.snapshot.SnapshotMapColumn)4 AssetSpecification (bio.terra.service.dataset.AssetSpecification)3 CorruptMetadataException (bio.terra.service.snapshot.exception.CorruptMetadataException)3 HashMap (java.util.HashMap)3 DaoKeyHolder (bio.terra.common.DaoKeyHolder)2 SnapshotRequestContentsModel (bio.terra.model.SnapshotRequestContentsModel)2 SnapshotRequestRowIdModel (bio.terra.model.SnapshotRequestRowIdModel)2 SnapshotRequestRowIdTableModel (bio.terra.model.SnapshotRequestRowIdTableModel)2 Field (com.google.cloud.bigquery.Field)2 TableId (com.google.cloud.bigquery.TableId)2