Search in sources :

Example 16 with Column

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

the class SnapshotService method conjureSnapshotTablesFromAsset.

/**
 * Magic up the snapshot tables and snapshot map from the asset tables and columns.
 * This method sets the table lists into snapshot and snapshotSource.
 *
 * @param asset  the one and only asset specification for this snapshot
 * @param snapshot snapshot to point back to and fill in
 * @param snapshotSource snapshotSource to point back to and fill in
 */
private void conjureSnapshotTablesFromAsset(AssetSpecification asset, Snapshot snapshot, SnapshotSource snapshotSource) {
    List<SnapshotTable> tableList = new ArrayList<>();
    List<SnapshotMapTable> mapTableList = new ArrayList<>();
    for (AssetTable assetTable : asset.getAssetTables()) {
        // Create early so we can hook up back pointers.
        SnapshotTable table = new SnapshotTable();
        // Build the column lists in parallel, so we can easily connect the
        // map column to the snapshot column.
        List<Column> columnList = new ArrayList<>();
        List<SnapshotMapColumn> mapColumnList = new ArrayList<>();
        for (AssetColumn assetColumn : assetTable.getColumns()) {
            Column column = new Column(assetColumn.getDatasetColumn());
            columnList.add(column);
            mapColumnList.add(new SnapshotMapColumn().fromColumn(assetColumn.getDatasetColumn()).toColumn(column));
        }
        table.name(assetTable.getTable().getName()).columns(columnList);
        tableList.add(table);
        mapTableList.add(new SnapshotMapTable().fromTable(assetTable.getTable()).toTable(table).snapshotMapColumns(mapColumnList));
    }
    snapshotSource.snapshotMapTables(mapTableList);
    snapshot.snapshotTables(tableList);
}
Also used : AssetTable(bio.terra.service.dataset.AssetTable) AssetColumn(bio.terra.service.dataset.AssetColumn) Column(bio.terra.common.Column) ArrayList(java.util.ArrayList) AssetColumn(bio.terra.service.dataset.AssetColumn)

Example 17 with Column

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

the class BigQueryPdao method mapValuesToRows.

// compute the row ids from the input ids and validate all inputs have matches
// returns a structure with the matching row ids (suitable for calling create snapshot)
// and any mismatched input values that don't have corresponding roww.
// NOTE: In the fullness of time, we may not do this and kick the function into the UI.
// So this code assumes there is one source and one set of input values.
// The query it builds embeds data values into the query in an array. I think it will
// support about 25,000 input values. If that is not enough there is another, more
// complicated alternative:
// - create a scratch table at snapshot creation time
// - truncate before we start
// - load the values in
// - do the query
// - truncate (even tidier...)
// So if we need to make this work in the long term, we can take that approach.
@Override
public RowIdMatch mapValuesToRows(Snapshot snapshot, SnapshotSource source, List<String> inputValues) throws InterruptedException {
    // One source: grab it and navigate to the relevant parts
    BigQueryProject bigQueryProject = bigQueryProjectForSnapshot(snapshot);
    AssetSpecification asset = source.getAssetSpecification();
    Column column = asset.getRootColumn().getDatasetColumn();
    ST sqlTemplate = new ST(mapValuesToRowsTemplate);
    sqlTemplate.add("project", bigQueryProject.getProjectId());
    sqlTemplate.add("dataset", prefixName(source.getDataset().getName()));
    sqlTemplate.add("table", column.getTable().getName());
    sqlTemplate.add("column", column.getName());
    sqlTemplate.add("inputVals", inputValues);
    // Execute the query building the row id match structure that tracks the matching
    // ids and the mismatched ids
    RowIdMatch rowIdMatch = new RowIdMatch();
    String sql = sqlTemplate.render();
    logger.debug("mapValuesToRows sql: " + sql);
    TableResult result = bigQueryProject.query(sql);
    for (FieldValueList row : result.iterateAll()) {
        // Test getting these by name
        FieldValue rowId = row.get(0);
        FieldValue inputValue = row.get(1);
        if (rowId.isNull()) {
            rowIdMatch.addMismatch(inputValue.getStringValue());
            logger.debug("rowId=<NULL>" + "  inVal=" + inputValue.getStringValue());
        } else {
            rowIdMatch.addMatch(inputValue.getStringValue(), rowId.getStringValue());
            logger.debug("rowId=" + rowId.getStringValue() + "  inVal=" + inputValue.getStringValue());
        }
    }
    return rowIdMatch;
}
Also used : ST(org.stringtemplate.v4.ST) TableResult(com.google.cloud.bigquery.TableResult) RowIdMatch(bio.terra.service.snapshot.RowIdMatch) SnapshotMapColumn(bio.terra.service.snapshot.SnapshotMapColumn) Column(bio.terra.common.Column) AssetSpecification(bio.terra.service.dataset.AssetSpecification) FieldValueList(com.google.cloud.bigquery.FieldValueList) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 18 with Column

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

the class AssetDao method retrieveAssetSpecifications.

// also retrieves dependent objects
public List<AssetSpecification> retrieveAssetSpecifications(Dataset dataset) {
    Map<UUID, DatasetTable> allTables = dataset.getTablesById();
    Map<UUID, Column> allColumns = dataset.getAllColumnsById();
    Map<UUID, Relationship> allRelationships = dataset.getRelationshipsById();
    String sql = "SELECT id, name, root_table_id, root_column_id FROM asset_specification WHERE dataset_id = " + ":datasetId";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("datasetId", dataset.getId());
    return jdbcTemplate.query(sql, params, (rs, rowNum) -> {
        UUID specId = rs.getObject("id", UUID.class);
        AssetSpecification spec = new AssetSpecification().id(specId).name(rs.getString("name"));
        spec.assetTables(new ArrayList(retrieveAssetTablesAndColumns(spec, rs.getObject("root_table_id", UUID.class), rs.getObject("root_column_id", UUID.class), allTables, allColumns)));
        spec.assetRelationships(retrieveAssetRelationships(spec.getId(), allRelationships));
        return spec;
    });
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Column(bio.terra.common.Column) Relationship(bio.terra.common.Relationship) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

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