Skip to content

Commit

Permalink
refactor(duckdb): use replace to generate less sql (#9713)
Browse files Browse the repository at this point in the history
Refactors the duckdb output to geospatial to use `* REPLACE` syntax to
generate less code when replacing a small number of geospatial columns
(usually one) among an otherwise large set of columns. Additionally, we
now only need to loop over expression columns once.
  • Loading branch information
cpcloud authored Jul 29, 2024
1 parent 038a95a commit f89aa32
Showing 11 changed files with 31 additions and 19 deletions.
20 changes: 11 additions & 9 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
@@ -101,23 +101,25 @@ def _to_sqlglot(
sql = super()._to_sqlglot(expr, limit=limit, params=params)

table_expr = expr.as_table()
geocols = frozenset(
geocols = [
name for name, typ in table_expr.schema().items() if typ.is_geospatial()
)
]

if not geocols:
return sql
else:
self._load_extensions(["spatial"])

compiler = self.compiler
quoted = compiler.quoted
return sg.select(
*(
self.compiler.f.st_aswkb(
sg.column(col, quoted=self.compiler.quoted)
).as_(col)
if col in geocols
else col
for col in table_expr.columns
sge.Star(
replace=[
compiler.f.st_aswkb(sg.column(col, quoted=quoted)).as_(
col, quoted=quoted
)
for col in geocols
]
)
).from_(sql.subquery())

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("p") AS p
*
REPLACE (ST_ASWKB("p") AS "p")
FROM (
SELECT
ST_GEOMFROMTEXT('POINT (1 0)') AS "p"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("p") AS p
*
REPLACE (ST_ASWKB("p") AS "p")
FROM (
SELECT
ST_GEOMFROMTEXT('POINT (1 0)') AS "p"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('POINT (0 0)') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('POINT (1 1)') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('POINT (2 2)') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('LINESTRING (0 0, 1 1, 2 2)') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('LINESTRING (2 2, 1 1, 0 0)') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('POLYGON ((0 0, 1 1, 2 2, 0 0))') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('MULTIPOLYGON (((0 0, 1 1, 2 2, 0 0)))') AS "result"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT
ST_ASWKB("result") AS result
*
REPLACE (ST_ASWKB("result") AS "result")
FROM (
SELECT
ST_GEOMFROMTEXT('MULTILINESTRING ((0 0, 1 1, 2 2), (2 2, 1 1, 0 0))') AS "result"

0 comments on commit f89aa32

Please sign in to comment.