Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow named arguments to 'pass through' in translation to SQL? #1574

Open
cboettig opened this issue Jan 26, 2025 · 2 comments
Open

allow named arguments to 'pass through' in translation to SQL? #1574

cboettig opened this issue Jan 26, 2025 · 2 comments

Comments

@cboettig
Copy link

Most of the time dbplyr is very good about letting functions known to the database backend (e.g. duckdb with spatial extension), to just "pass through", e.g. if tbl is a lazy table to a duckdb extension, we can do:

tbl |> 
  mutate(geom = st_transform(geom, "EPSG:4326", "EPSG:3857")) 

because st_transform() is understood by duckdb.

(Full reprex here: cboettig/duckdbfs#34).

But in this particular case, DuckDB requires an extra argument for working with EPSG:4326, as described in the docs. We want the SQL query to render as:

SELECT ST_TRANSFORM(geom,  'EPSG:4326',  'EPSG:3857', always_xy := true) AS geom

But I cannot see how to get the always_xy := true part to render properly. E.g. if we try this:

tbl |> 
  mutate(geom = st_transform(geom, "EPSG:4326", "EPSG:3857", always_xy = TRUE)) 

dbplyr gives us this kinda nonsense SQL instead:

SELECT ST_TRANSFORM(geom,  'EPSG:4326',  'EPSG:3857',  TRUE AS always_xy) AS geom

The docs just suggest that "unknown functions translate "as is" but that does not seem to be what happens here.

Thanks!

@ejneer
Copy link
Contributor

ejneer commented Feb 16, 2025

Not immediately clear how this could be generally addressed without invasive changes. But, aside from having to "teach" dbplyr about each particular function by defining them in an appropriate backend, I think you can get there with inlining some sql. e.g.:

pad_tract |>
  select(STATE, FIPS, geom) |>
  filter(STATE == "California") |> 
  distinct() |>
  mutate(geom = st_transform(geom, "EPSG:4326", "EPSG:3857", sql("always_xy := TRUE"))) |> show_query()

returning:

SELECT
  STATE,
  FIPS,
  st_transform(geom, 'EPSG:4326', 'EPSG:3857', always_xy := TRUE) AS geom
FROM (
  SELECT DISTINCT STATE, FIPS, geom
  FROM ecwqshpcycckzfg
  WHERE (STATE = 'California')
) q01

@cboettig
Copy link
Author

@ejneer Very clever! I think this is a good enough escape hatch for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants