Skip to content

Commit

Permalink
feat(bigquery): enable use of GEOGRAPHY query params (#2158)
Browse files Browse the repository at this point in the history
* feat(bigquery): enable use of GEOGRAPHY query params
  • Loading branch information
shollyman committed Jul 15, 2022
1 parent 970135b commit b19ad76
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Expand Up @@ -258,6 +258,11 @@ public static QueryParameterValue string(String value) {
return of(value, StandardSQLTypeName.STRING);
}

/** Creates a {@code QueryParameterValue} object with a type of GEOGRAPHY. */
public static QueryParameterValue geography(String value) {
return of(value, StandardSQLTypeName.GEOGRAPHY);
}

/**
* Creates a {@code QueryParameterValue} object with a type of JSON. Currently, this is only
* supported in INSERT, not in query as a filter
Expand Down Expand Up @@ -369,6 +374,8 @@ private static <T> StandardSQLTypeName classToType(Class<T> type) {
return StandardSQLTypeName.BOOL;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.STRING;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.GEOGRAPHY;
} else if (Integer.class.isAssignableFrom(type)) {
return StandardSQLTypeName.INT64;
} else if (Long.class.isAssignableFrom(type)) {
Expand Down Expand Up @@ -422,6 +429,8 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
break;
case STRING:
return value.toString();
case GEOGRAPHY:
return value.toString();
case JSON:
if (value instanceof String || value instanceof JsonObject) return value.toString();
case INTERVAL:
Expand Down
Expand Up @@ -196,6 +196,15 @@ public void testString() {
assertThat(value.getArrayValues()).isNull();
}

@Test
public void testGeography() {
QueryParameterValue value = QueryParameterValue.geography("POINT(-122.350220 47.649154)");
assertThat(value.getValue()).isEqualTo("POINT(-122.350220 47.649154)");
assertThat(value.getType()).isEqualTo(StandardSQLTypeName.GEOGRAPHY);
assertThat(value.getArrayType()).isNull();
assertThat(value.getArrayValues()).isNull();
}

@Test
public void testJson() {
QueryParameterValue value =
Expand Down
Expand Up @@ -3655,6 +3655,28 @@ public void testBytesParameter() throws Exception {
assertEquals(1, rowCount);
}

@Test
public void testGeographyParameter() throws Exception {
// Issues a simple ST_DISTANCE using two geopoints, one being a named geography parameter.
String query =
"SELECT ST_DISTANCE(ST_GEOGFROMTEXT(\"POINT(-122.335503 47.625536)\"), @geo) < 3000 as within3k";
QueryParameterValue geoParameterValue =
QueryParameterValue.geography("POINT(-122.3509153 47.6495389)");
QueryJobConfiguration config =
QueryJobConfiguration.newBuilder(query)
.setDefaultDataset(DatasetId.of(DATASET))
.setUseLegacySql(false)
.addNamedParameter("geo", geoParameterValue)
.build();
TableResult result = bigquery.query(config);
int rowCount = 0;
for (FieldValueList row : result.getValues()) {
rowCount++;
assertEquals(true, row.get(0).getBooleanValue());
}
assertEquals(1, rowCount);
}

@Test
public void testListJobs() {
Page<Job> jobs = bigquery.listJobs();
Expand Down

0 comments on commit b19ad76

Please sign in to comment.