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

Export a mask akin to rlang::.data for custom SQL functions #1117

Open
MichaelChirico opened this issue Jan 31, 2023 · 5 comments
Open

Export a mask akin to rlang::.data for custom SQL functions #1117

MichaelChirico opened this issue Jan 31, 2023 · 5 comments
Labels
feature a feature request or enhancement func trans 🌍 Translation of individual functions to SQL

Comments

@MichaelChirico
Copy link
Contributor

A useful feature of dbplyr is automatically passing through unknown calls straight into SQL, e.g. some flavors support ANY_VALUE() to take a value from a column arbitrarily during aggregation:

conn |>
  tbl("my_schema.table") |>
  group_by(grp) |>
  summarize(some_x = ANY_VALUE(x)) |>
  ungroup() |>
  show_query()

Will give something like

SELECT grp, ANY_VALUE(x) AS some_x
FROM my_schema.tbl
GROUP BY grp

However, within packages, this presents the classic issue with R CMD check notes that ANY_VALUE() is an undefined function, from the static R checker's point of view.

rlang offers .data and .env masks for this purpose:

https://rlang.r-lib.org/reference/dot-data.html

Could dbplyr offer something similar, e.g. an .sql mask we could use as:

conn |>
  tbl("my_schema.table") |>
  group_by(grp) |>
  summarize(some_x = .sql$ANY_VALUE(x)) |>
  ungroup() |>
  show_query()
@MichaelChirico
Copy link
Contributor Author

Noting that this won't cover NSE-only operators like %like% that support custom translation, maybe a simple placeholder should be exported for those?

@ablack3

This comment was marked as off-topic.

@mgirlich
Copy link
Collaborator

@hadley What do you think?

@hadley
Copy link
Member

hadley commented Oct 27, 2023

Hmmmm, exposing .sql is an interesting idea. We already have a way to force remote execution with sql() this would be more R CMD check friendly.

I quite like the idea of exporting a %like% place holder too — it could just work like dplyr::n() and throw an error when called directly.

@hadley hadley added feature a feature request or enhancement func trans 🌍 Translation of individual functions to SQL labels Nov 2, 2023
@hadley
Copy link
Member

hadley commented Jan 10, 2024

Given that .env$mean(x) clearly implies that we should evaluate mean() locally, I wonder if it's not too much of an abuse for .data$mean(x) to mean evaluate mean(x) in the database? (i.e. translate it to SQL)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement func trans 🌍 Translation of individual functions to SQL
Projects
None yet
Development

No branches or pull requests

4 participants