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

pmin and pmax fail in mutate connected to SQL Server #3227

Closed
jnolis opened this issue Nov 28, 2017 · 9 comments
Closed

pmin and pmax fail in mutate connected to SQL Server #3227

jnolis opened this issue Nov 28, 2017 · 9 comments
Assignees
Labels
feature a feature request or enhancement

Comments

@jnolis
Copy link

jnolis commented Nov 28, 2017

Occasionally, I will need to use pmin or pmax to replace values above or below a threshold, for instance:

require(dplyr)
data_frame(X = 1:10) %>%
  mutate(Y = pmax(X,5))

However, when I use dbplyr to try and do this using a SQL Server table, I get the following error:

tbl(con,"OrderInfo") %>%
  mutate(Test = pmax(Revenue,0))
Error: <SQL> 'SELECT  TOP 10 "CustomerId", "OrderId", "ClientOrderId", "Channel", "OrderDate", "Revenue", "Cost", MAX("Revenue", 0.0) AS "Test"
FROM "OrderInfo"'
  nanodbc/nanodbc.cpp:1587: 42000: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]The MAX function requires 1 argument(s). 

As you can see, the SQL code generator assumes that I want to do a pairwise MAX, however SQL Server does not support that (only taking the maximum over a single column).

A cursory Stack Overflow search suggests that to do this operation a case statement could be used. I think either the SQL code generator should either be updated to do this, or an error should be generated at the time of calling the mutate.

@edgararuiz-zz
Copy link

You're correct, pmax and pmin is not currently supported. So this is a feature request. Thank you for the SO link.

@krlmlr krlmlr added database feature a feature request or enhancement labels Dec 12, 2017
@DavisVaughan
Copy link
Member

DavisVaughan commented Dec 23, 2017

@edgararuiz, You guys could take the approach I used for Access. It only works for two columns, but that is the most common case.

                   # pmin/pmax for 2 columns
                   pmin          = function(x, y) {
                     sql_expr(iif(UQ(x) <= UQ(y), !!x, !!y))
                   },

                   pmax          = function(x, y) {
                     sql_expr(iif(UQ(x) <= UQ(y), !!y, !!x))
                   },

@krlmlr
Copy link
Member

krlmlr commented Jan 17, 2018

@DavisVaughan: Macro expansion is a slippery slope, what if x and y are complex expressions?

Out of curiosity: Which databases actually support pairwise MIN() and MAX()? Sqlite3 does, Postgres and MySQL don't. Maybe we should just remove the default translation of pmin and pmax and enable it only for the databases that support this?

@edgararuiz-zz
Copy link

You're right, we would enable it only for those who support it. I believe can also do pmin and pmax

@DavisVaughan
Copy link
Member

DavisVaughan commented Jan 17, 2018

For completeness, Postgres and MySQL have GREATEST / LEAST which do what you want.
dbGetQuery(con, "SELECT GREATEST(drat, wt) FROM mtcars").
Works with any number of columns like pmax / pmin. This should probably be implemented right? (No such function for MSSQL)

Docs for Postgres at the bottom here

Docs for MySQL here

A stack overflow Q/A confirming the equivalence of Postgres GREATEST and SQLite pairwise max() here.

@hadley
Copy link
Member

hadley commented Jun 7, 2018

Let's remove it from the default translation, and add to Postgres, MySQL, and SQLite.

Is it worth making the default translation an error?

@krlmlr
Copy link
Member

krlmlr commented Jun 18, 2018

If we don't translate pmin() and pmax() (and apply dbplyr's default passthrough for unknown functions), the database gives a helpful error message. It didn't work before, so by changing the error message we don't introduce any unwanted behavior, I think.

@ghost
Copy link

ghost commented Jun 25, 2018

This issue was moved by krlmlr to tidyverse/dbplyr/issues/118.

@ghost ghost closed this as completed Jun 25, 2018
@lock
Copy link

lock bot commented Dec 22, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 22, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants