Search in sources :

Example 1 with QueryParameterValue

use of com.google.cloud.bigquery.QueryParameterValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testPositionalQueryParameters.

@Test
public void testPositionalQueryParameters() throws InterruptedException {
    String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.getTable()).append(" WHERE StringField = ?").append(" AND TimestampField > ?").append(" AND IntegerField IN UNNEST(?)").append(" AND IntegerField < ?").append(" AND FloatField > ?").toString();
    QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
    QueryParameterValue timestampParameter = QueryParameterValue.timestamp("2014-01-01 07:00:00.000000+00:00");
    QueryParameterValue intArrayParameter = QueryParameterValue.array(new Integer[] { 3, 4 }, Integer.class);
    QueryParameterValue int64Parameter = QueryParameterValue.int64(5);
    QueryParameterValue float64Parameter = QueryParameterValue.float64(0.5);
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).setUseLegacySql(false).addPositionalParameter(stringParameter).addPositionalParameter(timestampParameter).addPositionalParameter(intArrayParameter).addPositionalParameter(int64Parameter).addPositionalParameter(float64Parameter).build();
    QueryResponse response = queryAndWaitForResponse(request);
    assertEquals(QUERY_RESULT_SCHEMA, response.getResult().getSchema());
    assertEquals(2, Iterables.size(response.getResult().getValues()));
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) Test(org.junit.Test)

Example 2 with QueryParameterValue

use of com.google.cloud.bigquery.QueryParameterValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testBytesParameter.

@Test
public void testBytesParameter() throws Exception {
    String query = new StringBuilder().append("SELECT BYTE_LENGTH(@p) AS length").toString();
    QueryParameterValue bytesParameter = QueryParameterValue.bytes(new byte[] { 1, 3 });
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).setUseLegacySql(false).addNamedParameter("p", bytesParameter).build();
    QueryResponse response = queryAndWaitForResponse(request);
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        rowCount++;
        assertEquals(2, row.get(0).getLongValue());
    }
    assertEquals(1, rowCount);
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 3 with QueryParameterValue

use of com.google.cloud.bigquery.QueryParameterValue in project workbench by all-of-us.

the class CodesQueryBuilder method buildQueryJobConfig.

@Override
public QueryJobConfiguration buildQueryJobConfig(QueryParameters params) {
    Map<GroupType, ListMultimap<String, SearchParameter>> paramMap = getMappedParameters(params.getParameters());
    List<String> queryParts = new ArrayList<String>();
    Map<String, QueryParameterValue> queryParams = new HashMap<>();
    for (GroupType group : paramMap.keySet()) {
        ListMultimap<String, SearchParameter> domainMap = paramMap.get(group);
        for (String domain : domainMap.keySet()) {
            final List<SearchParameter> searchParameterList = domainMap.get(domain);
            final SearchParameter parameter = searchParameterList.get(0);
            final String type = parameter.getType();
            final String subtype = parameter.getSubtype();
            List<String> codes = searchParameterList.stream().map(SearchParameter::getValue).collect(Collectors.toList());
            if (group.equals(GroupType.NOT_GROUP)) {
                buildNotGroupQuery(type, subtype, queryParts, queryParams, domain, codes);
            } else {
                buildGroupQuery(type, subtype, queryParts, queryParams, domain, codes);
            }
        }
    }
    String finalSql = OUTER_SQL_TEMPLATE.replace("${innerSql}", String.join(UNION_TEMPLATE, queryParts));
    return QueryJobConfiguration.newBuilder(finalSql).setNamedParameters(queryParams).setUseLegacySql(false).build();
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) SearchParameter(org.pmiops.workbench.model.SearchParameter)

Example 4 with QueryParameterValue

use of com.google.cloud.bigquery.QueryParameterValue in project workbench by all-of-us.

the class CodesQueryBuilderTest method buildQueryJobConfigICD9Group.

@Test
public void buildQueryJobConfigICD9Group() throws Exception {
    String procedureNamedParameter = "";
    String cmProcedureParameter = "";
    String procProcedureParameter = "";
    String conditionNamedParameter = "";
    String cmConditionParameter = "";
    String procConditionParameter = "";
    String measurementNamedParameter = "";
    String cmMeasurementParameter = "";
    String procMeasurementParameter = "";
    List<SearchParameter> params = new ArrayList<>();
    params.add(new SearchParameter().group(false).type("ICD9").domain("Condition").value("10.1"));
    params.add(new SearchParameter().group(false).type("ICD9").domain("Condition").value("20.2"));
    params.add(new SearchParameter().group(true).type("ICD9").domain("Measurement").value("0"));
    params.add(new SearchParameter().group(true).type("ICD9").domain("Procedure").value("1"));
    /* Check the generated querybuilder */
    QueryJobConfiguration queryJobConfiguration = queryBuilder.buildQueryJobConfig(new QueryParameters().type("ICD9").parameters(params));
    for (String key : queryJobConfiguration.getNamedParameters().keySet()) {
        if (key.startsWith("Condition")) {
            conditionNamedParameter = key;
            cmConditionParameter = "cm" + key.replace("Condition", "");
            procConditionParameter = "proc" + key.replace("Condition", "");
        } else if (key.startsWith("Measurement")) {
            measurementNamedParameter = key;
            cmMeasurementParameter = "cm" + key.replace("Measurement", "");
            procMeasurementParameter = "proc" + key.replace("Measurement", "");
        } else if (key.startsWith("Procedure")) {
            procedureNamedParameter = key;
            cmProcedureParameter = "cm" + key.replace("Procedure", "");
            procProcedureParameter = "proc" + key.replace("Procedure", "");
        }
    }
    String expected = "select person_id\n" + "from `${projectId}.${dataSetId}.person` p\n" + "where person_id in (select person_id\n" + "from `${projectId}.${dataSetId}.measurement` a, `${projectId}.${dataSetId}.concept` b\n" + "where a.measurement_source_concept_id = b.concept_id\n" + "and b.vocabulary_id in (@" + cmMeasurementParameter + ",@" + procMeasurementParameter + ")\n" + "and b.concept_code like @" + measurementNamedParameter + "\n" + " union all\n" + "select person_id\n" + "from `${projectId}.${dataSetId}.procedure_occurrence` a, `${projectId}.${dataSetId}.concept` b\n" + "where a.procedure_source_concept_id = b.concept_id\n" + "and b.vocabulary_id in (@" + cmProcedureParameter + ",@" + procProcedureParameter + ")\n" + "and b.concept_code like @" + procedureNamedParameter + "\n" + " union all\n" + "select person_id\n" + "from `${projectId}.${dataSetId}.condition_occurrence` a, `${projectId}.${dataSetId}.concept` b\n" + "where a.condition_source_concept_id = b.concept_id\n" + "and b.vocabulary_id in (@" + cmConditionParameter + ",@" + procConditionParameter + ")\n" + "and b.concept_code in unnest(@" + conditionNamedParameter + ")\n" + ")\n";
    assertEquals(expected, queryJobConfiguration.getQuery());
    /* Check the querybuilder parameters */
    List<QueryParameterValue> conditionCodes = queryJobConfiguration.getNamedParameters().get(conditionNamedParameter).getArrayValues();
    assertTrue(conditionCodes.contains(QueryParameterValue.newBuilder().setValue("10.1").setType(StandardSQLTypeName.STRING).build()));
    assertTrue(conditionCodes.contains(QueryParameterValue.newBuilder().setValue("20.2").setType(StandardSQLTypeName.STRING).build()));
    String measurementCode = queryJobConfiguration.getNamedParameters().get(measurementNamedParameter).getValue();
    assertTrue("0%".equals(measurementCode));
    String procedureCode = queryJobConfiguration.getNamedParameters().get(procedureNamedParameter).getValue();
    assertTrue("1%".equals(procedureCode));
    assertEquals("ICD9CM", queryJobConfiguration.getNamedParameters().get(cmConditionParameter).getValue());
    assertEquals("ICD9Proc", queryJobConfiguration.getNamedParameters().get(procConditionParameter).getValue());
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) ArrayList(java.util.ArrayList) SearchParameter(org.pmiops.workbench.model.SearchParameter) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Test(org.junit.Test)

Example 5 with QueryParameterValue

use of com.google.cloud.bigquery.QueryParameterValue in project workbench by all-of-us.

the class CodesQueryBuilderTest method buildQueryJobConfigICD9NotGroup.

@Test
public void buildQueryJobConfigICD9NotGroup() throws Exception {
    String measurementNamedParameter = "";
    String conditionNamedParameter = "";
    String cmConditionParameter = "";
    String procConditionParameter = "";
    String cmMeasurementParameter = "";
    String procMeasurementParameter = "";
    List<SearchParameter> params = new ArrayList<>();
    params.add(new SearchParameter().group(false).type("ICD9").domain("Condition").value("10.1"));
    params.add(new SearchParameter().group(false).type("ICD9").domain("Condition").value("20.2"));
    params.add(new SearchParameter().group(false).type("ICD9").domain("Measurement").value("30.3"));
    /* Check the generated querybuilder */
    QueryJobConfiguration queryJobConfiguration = queryBuilder.buildQueryJobConfig(new QueryParameters().type("ICD9").parameters(params));
    for (String key : queryJobConfiguration.getNamedParameters().keySet()) {
        if (key.startsWith("Condition")) {
            conditionNamedParameter = key;
            cmConditionParameter = "cm" + key.replace("Condition", "");
            procConditionParameter = "proc" + key.replace("Condition", "");
        } else if (key.startsWith("Measurement")) {
            measurementNamedParameter = key;
            cmMeasurementParameter = "cm" + key.replace("Measurement", "");
            procMeasurementParameter = "proc" + key.replace("Measurement", "");
        }
    }
    String expected = "select person_id\n" + "from `${projectId}.${dataSetId}.person` p\n" + "where person_id in (select person_id\n" + "from `${projectId}.${dataSetId}.condition_occurrence` a, `${projectId}.${dataSetId}.concept` b\n" + "where a.condition_source_concept_id = b.concept_id\n" + "and b.vocabulary_id in (@" + cmConditionParameter + ",@" + procConditionParameter + ")\n" + "and b.concept_code in unnest(@" + conditionNamedParameter + ")\n" + " union all\n" + "select person_id\n" + "from `${projectId}.${dataSetId}.measurement` a, `${projectId}.${dataSetId}.concept` b\n" + "where a.measurement_source_concept_id = b.concept_id\n" + "and b.vocabulary_id in (@" + cmMeasurementParameter + ",@" + procMeasurementParameter + ")\n" + "and b.concept_code in unnest(@" + measurementNamedParameter + ")\n" + ")\n";
    assertEquals(expected, queryJobConfiguration.getQuery());
    /* Check the querybuilder parameters */
    List<QueryParameterValue> conditionCodes = queryJobConfiguration.getNamedParameters().get(conditionNamedParameter).getArrayValues();
    assertTrue(conditionCodes.contains(QueryParameterValue.newBuilder().setValue("10.1").setType(StandardSQLTypeName.STRING).build()));
    assertTrue(conditionCodes.contains(QueryParameterValue.newBuilder().setValue("20.2").setType(StandardSQLTypeName.STRING).build()));
    List<QueryParameterValue> measurementCodes = queryJobConfiguration.getNamedParameters().get(measurementNamedParameter).getArrayValues();
    assertTrue(measurementCodes.contains(QueryParameterValue.newBuilder().setValue("30.3").setType(StandardSQLTypeName.STRING).build()));
    assertEquals("ICD9CM", queryJobConfiguration.getNamedParameters().get(cmConditionParameter).getValue());
    assertEquals("ICD9Proc", queryJobConfiguration.getNamedParameters().get(procConditionParameter).getValue());
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) ArrayList(java.util.ArrayList) SearchParameter(org.pmiops.workbench.model.SearchParameter) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Test(org.junit.Test)

Aggregations

QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)9 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 SearchParameter (org.pmiops.workbench.model.SearchParameter)4 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)3 QueryRequest (com.google.cloud.bigquery.QueryRequest)3 QueryResponse (com.google.cloud.bigquery.QueryResponse)3 HashMap (java.util.HashMap)3 FieldValue (com.google.cloud.bigquery.FieldValue)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)1 ColumnFilter (org.pmiops.workbench.model.ColumnFilter)1 ParticipantConditionsColumns (org.pmiops.workbench.model.ParticipantConditionsColumns)1 TableQuery (org.pmiops.workbench.model.TableQuery)1