Search in sources :

Example 31 with ColumnFilter

use of org.pmiops.workbench.model.ColumnFilter in project workbench by all-of-us.

the class FieldSetQueryBuilder method buildQuery.

public QueryJobConfiguration buildQuery(ParticipantCriteria participantCriteria, TableQueryAndConfig tableQueryAndConfig, long resultSize, long offset) {
    TableQuery tableQuery = tableQueryAndConfig.getTableQuery();
    List<String> columnNames = tableQuery.getColumns();
    String tableName = tableQuery.getTableName();
    StringBuilder startSql = new StringBuilder("select ");
    // TODO: add column aliases, use below
    startSql.append(Joiner.on(", ").join(columnNames));
    startSql.append("\nfrom `${projectId}.${dataSetId}.");
    startSql.append(tableName);
    startSql.append("` ");
    startSql.append(tableName);
    startSql.append("\nwhere\n");
    Map<String, QueryParameterValue> paramMap = new HashMap<>();
    List<List<ColumnFilter>> columnFilters = tableQuery.getFilters();
    if (columnFilters != null && !columnFilters.isEmpty()) {
        startSql.append("(");
        boolean first = true;
        for (List<ColumnFilter> filterList : columnFilters) {
            if (first) {
                first = false;
            } else {
                startSql.append("\nor\n");
            }
            handleColumnFilters(filterList, tableQueryAndConfig, startSql, paramMap);
        }
        startSql.append(")\nand\n");
    }
    StringBuilder endSql = new StringBuilder("order by ");
    List<String> orderBy = tableQuery.getOrderBy();
    if (orderBy.isEmpty()) {
        throw new BadRequestException("Order by list must not be empty");
    }
    endSql.append(Joiner.on(", ").join(orderBy));
    endSql.append(" limit ");
    endSql.append(resultSize);
    if (offset > 0) {
        endSql.append(" offset ");
        endSql.append(offset);
    }
    return participantCounter.buildQuery(participantCriteria, startSql.toString(), endSql.toString(), tableName, paramMap);
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) HashMap(java.util.HashMap) ColumnFilter(org.pmiops.workbench.model.ColumnFilter) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) TableQuery(org.pmiops.workbench.model.TableQuery) List(java.util.List)

Aggregations

ColumnFilter (org.pmiops.workbench.model.ColumnFilter)31 TableQuery (org.pmiops.workbench.model.TableQuery)30 Test (org.junit.Test)29 BigQueryBaseTest (org.pmiops.workbench.api.BigQueryBaseTest)29 FieldSet (org.pmiops.workbench.model.FieldSet)29 MaterializeCohortResponse (org.pmiops.workbench.model.MaterializeCohortResponse)29 BigDecimal (java.math.BigDecimal)8 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)2 QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)1 HashMap (java.util.HashMap)1 List (java.util.List)1