Skip to content

Commit bd8c93f

Browse files
authored
fix(bigquery): repr geospatial values in interactive mode (#9712)
1 parent 9751a36 commit bd8c93f

File tree

14 files changed

+117
-24
lines changed

14 files changed

+117
-24
lines changed

ibis/backends/bigquery/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,36 @@ def _to_sqlglot(
683683
self._define_udf_translation_rules(expr)
684684
sql = super()._to_sqlglot(expr, limit=limit, params=params, **kwargs)
685685

686+
table_expr = expr.as_table()
687+
geocols = [
688+
name for name, typ in table_expr.schema().items() if typ.is_geospatial()
689+
]
690+
686691
query = sql.transform(
687692
_qualify_memtable,
688693
dataset=getattr(self._session_dataset, "dataset_id", None),
689694
project=getattr(self._session_dataset, "project", None),
690695
).transform(_remove_null_ordering_from_unsupported_window)
691-
return query
696+
697+
if not geocols:
698+
return query
699+
700+
# if there are any geospatial columns, we have to convert them to WKB,
701+
# so interactive mode knows how to display them
702+
#
703+
# by default bigquery returns data to python as WKT, and there's really
704+
# no point in supporting both if we don't need to.
705+
compiler = self.compiler
706+
quoted = compiler.quoted
707+
f = compiler.f
708+
return sg.select(
709+
sge.Star(
710+
replace=[
711+
f.st_asbinary(sg.column(col, quoted=quoted)).as_(col, quoted=quoted)
712+
for col in geocols
713+
]
714+
)
715+
).from_(query.subquery())
692716

693717
def raw_sql(self, query: str, params=None, page_size: int | None = None):
694718
query_parameters = [

ibis/backends/bigquery/converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def convert_GeoSpatial(cls, s, dtype, pandas_type):
99
import geopandas as gpd
1010
import shapely as shp
1111

12-
return gpd.GeoSeries(shp.from_wkt(s))
12+
return gpd.GeoSeries(shp.from_wkb(s))
1313

1414
convert_Point = convert_LineString = convert_Polygon = convert_MultiLineString = (
1515
convert_MultiPoint

ibis/backends/bigquery/tests/system/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,17 @@ def test_complex_column_name(con):
455455
)
456456
result = con.to_pandas(expr)
457457
assert result == 1
458+
459+
460+
def test_geospatial_interactive(con, monkeypatch):
461+
pytest.importorskip("geopandas")
462+
463+
monkeypatch.setattr(ibis.options, "interactive", True)
464+
t = con.table("bigquery-public-data.geo_us_boundaries.zip_codes")
465+
expr = (
466+
t.filter(lambda t: t.zip_code_geom.geometry_type() == "ST_Polygon")
467+
.head(1)
468+
.zip_code_geom
469+
)
470+
result = repr(expr)
471+
assert "POLYGON" in result
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_difference(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_difference(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_intersection(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_intersection(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_union(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_union(`t0`.`geog0`, `t0`.`geog1`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_geogpoint(`t0`.`lon`, `t0`.`lat`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_geogpoint(`t0`.`lon`, `t0`.`lat`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_simplify(`t0`.`geog`, 5.2) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_simplify(`t0`.`geog`, 5.2) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_buffer(`t0`.`geog`, 5.2) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_buffer(`t0`.`geog`, 5.2) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_centroid(`t0`.`geog`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_centroid(`t0`.`geog`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_endpoint(`t0`.`geog`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_endpoint(`t0`.`geog`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_pointn(`t0`.`geog`, 3) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_pointn(`t0`.`geog`, 3) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_startpoint(`t0`.`geog`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_startpoint(`t0`.`geog`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
SELECT
2-
st_union_agg(`t0`.`geog`) AS `tmp`
3-
FROM `t` AS `t0`
2+
*
3+
REPLACE (st_asbinary(`tmp`) AS `tmp`)
4+
FROM (
5+
SELECT
6+
st_union_agg(`t0`.`geog`) AS `tmp`
7+
FROM `t` AS `t0`
8+
)

0 commit comments

Comments
 (0)