Search in sources :

Example 1 with ColumnConfig

use of org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig in project workbench by all-of-us.

the class FieldSetQueryBuilder method extractResults.

public Map<String, Object> extractResults(TableQueryAndConfig tableQueryAndConfig, List<FieldValue> row) {
    TableQuery tableQuery = tableQueryAndConfig.getTableQuery();
    List<ColumnConfig> columnConfigs = tableQuery.getColumns().stream().map(columnName -> tableQueryAndConfig.getColumn(columnName)).collect(Collectors.toList());
    Map<String, Object> results = new HashMap<>(tableQuery.getColumns().size());
    for (int i = 0; i < columnConfigs.size(); i++) {
        FieldValue fieldValue = row.get(i);
        ColumnConfig columnConfig = columnConfigs.get(i);
        if (!fieldValue.isNull()) {
            Object value;
            switch(columnConfig.type) {
                case DATE:
                    value = fieldValue.getStringValue();
                    break;
                case FLOAT:
                    value = fieldValue.getDoubleValue();
                    break;
                case INTEGER:
                    value = fieldValue.getLongValue();
                    break;
                case STRING:
                    value = fieldValue.getStringValue();
                    break;
                case TIMESTAMP:
                    value = DATE_TIME_FORMAT.print(fieldValue.getTimestampValue() / 1000L);
                    break;
                default:
                    throw new IllegalStateException("Unrecognized column type: " + columnConfig.type);
            }
            results.put(columnConfig.name, value);
        }
    }
    return results;
}
Also used : DateTimeFormat(org.joda.time.format.DateTimeFormat) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) FieldValue(com.google.cloud.bigquery.FieldValue) ColumnType(org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ColumnConfig(org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig) TableQuery(org.pmiops.workbench.model.TableQuery) Operator(org.pmiops.workbench.model.Operator) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) FieldSet(org.pmiops.workbench.model.FieldSet) Strings(com.google.common.base.Strings) BigDecimal(java.math.BigDecimal) List(java.util.List) Service(org.springframework.stereotype.Service) Map(java.util.Map) ColumnFilter(org.pmiops.workbench.model.ColumnFilter) ParseException(java.text.ParseException) QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) Joiner(com.google.common.base.Joiner) OperatorUtils(org.pmiops.workbench.utils.OperatorUtils) ColumnConfig(org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig) HashMap(java.util.HashMap) TableQuery(org.pmiops.workbench.model.TableQuery) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 2 with ColumnConfig

use of org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig in project workbench by all-of-us.

the class CohortMaterializationService method getTableQueryAndConfig.

private TableQueryAndConfig getTableQueryAndConfig(FieldSet fieldSet) {
    TableQuery tableQuery;
    if (fieldSet == null) {
        tableQuery = new TableQuery();
        tableQuery.setTableName(PERSON_TABLE);
        tableQuery.setColumns(ImmutableList.of(PERSON_ID));
    } else {
        tableQuery = fieldSet.getTableQuery();
        if (tableQuery == null) {
            // TODO: support other kinds of field sets besides tableQuery
            throw new BadRequestException("tableQuery must be specified in field sets");
        }
        String tableName = tableQuery.getTableName();
        if (Strings.isNullOrEmpty(tableName)) {
            throw new BadRequestException("Table name must be specified in field sets");
        }
    }
    CdrBigQuerySchemaConfig cdrSchemaConfig = cdrSchemaConfigProvider.get();
    TableConfig tableConfig = cdrSchemaConfig.cohortTables.get(tableQuery.getTableName());
    if (tableConfig == null) {
        throw new BadRequestException("Table " + tableQuery.getTableName() + " is not a valid " + "cohort table; valid tables are: " + cdrSchemaConfig.cohortTables.keySet().stream().sorted().collect(Collectors.joining(",")));
    }
    Map<String, ColumnConfig> columnMap = Maps.uniqueIndex(tableConfig.columns, columnConfig -> columnConfig.name);
    List<String> columnNames = tableQuery.getColumns();
    if (columnNames == null || columnNames.isEmpty()) {
        // By default, return all columns on the table in question in our configuration.
        tableQuery.setColumns(columnMap.keySet().stream().collect(Collectors.toList()));
    } else {
        for (String columnName : columnNames) {
            // TODO: handle columns on foreign key tables
            if (!columnMap.containsKey(columnName)) {
                throw new BadRequestException("Unrecognized column name: " + columnName);
            }
        }
    }
    List<String> orderBy = tableQuery.getOrderBy();
    if (orderBy == null || orderBy.isEmpty()) {
        ColumnConfig primaryKey = findPrimaryKey(tableConfig);
        if (PERSON_ID.equals(primaryKey)) {
            tableQuery.setOrderBy(ImmutableList.of(PERSON_ID));
        } else {
            // TODO: consider having per-table default sort order based on e.g. timestamp
            tableQuery.setOrderBy(ImmutableList.of(PERSON_ID, primaryKey.name));
        }
    } else {
        for (String columnName : orderBy) {
            if (columnName.toUpperCase().endsWith(DESCENDING_SUFFIX)) {
                columnName = columnName.substring(0, columnName.length() - DESCENDING_SUFFIX.length());
            }
            if (!columnMap.containsKey(columnName)) {
                throw new BadRequestException("Invalid column in orderBy: " + columnName);
            }
        }
    }
    return new TableQueryAndConfig(tableQuery, tableConfig, columnMap);
}
Also used : CdrBigQuerySchemaConfig(org.pmiops.workbench.config.CdrBigQuerySchemaConfig) ColumnConfig(org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig) TableQueryAndConfig(org.pmiops.workbench.cohortbuilder.TableQueryAndConfig) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) TableConfig(org.pmiops.workbench.config.CdrBigQuerySchemaConfig.TableConfig) TableQuery(org.pmiops.workbench.model.TableQuery)

Aggregations

ColumnConfig (org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnConfig)2 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)2 TableQuery (org.pmiops.workbench.model.TableQuery)2 FieldValue (com.google.cloud.bigquery.FieldValue)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)1 Joiner (com.google.common.base.Joiner)1 Strings (com.google.common.base.Strings)1 BigDecimal (java.math.BigDecimal)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 DateTimeFormat (org.joda.time.format.DateTimeFormat)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1 TableQueryAndConfig (org.pmiops.workbench.cohortbuilder.TableQueryAndConfig)1 CdrBigQuerySchemaConfig (org.pmiops.workbench.config.CdrBigQuerySchemaConfig)1 ColumnType (org.pmiops.workbench.config.CdrBigQuerySchemaConfig.ColumnType)1