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

MS SQL mutate(ifelse()) translation not working #93

Closed
bogdanrau opened this Issue Jun 1, 2018 · 8 comments

Comments

Projects
None yet
4 participants
@bogdanrau

bogdanrau commented Jun 1, 2018

I'm running into an issue when using mutate and ifelse (or if) on a SQL Server backend. The following sample query:

df <- tbl(con, in_schema("schema", "table_name")) %>%
   mutate(field = ifelse(field %in% c('test1', 'test2', 'test3'), 'string1', 'string2') %>%
   collect()

... will give the following translation:

SELECT CASE WHEN (("field" IN ('test1', 'test2', 'test3')) = 'TRUE') THEN ('string1') 
            WHEN (("field" IN ('test1', 'test2', 'test3')) =  'FALSE') THEN ('string2') 
            END AS "field"

Which will return an error. The correct translation should be:

SELECT CASE WHEN ("field" IN ('test1', 'test2', 'test3')) THEN 'string1' 
            WHEN ("field" NOT IN ('test1', 'test2', 'test3')) THEN 'string2' 
            END AS "field"

Will correcting this in the mssql_if function create any issues in other queries, or is there a better way to do mutate(ifelse()) in mssql?

mssql_sql_if <- function(cond, if_true, if_false = NULL) {

@bogdanrau

This comment has been minimized.

bogdanrau commented Jun 1, 2018

Sorry guys. Just opened up a ticket in dplyr instead (as suggested in the readme). Closing this guy. Opened tidyverse/dplyr#3632

@bogdanrau bogdanrau closed this Jun 1, 2018

@hadley hadley reopened this Jun 7, 2018

@hadley

This comment has been minimized.

Member

hadley commented Jun 7, 2018

No you should file here; I need to update the readme.

@bogdanrau

This comment has been minimized.

bogdanrau commented Jun 14, 2018

Thanks Hadley. @edgararuiz, any insight on what a fix might be?

@edgararuiz

This comment has been minimized.

Collaborator

edgararuiz commented Jun 18, 2018

Ok, I think that the update may have to be done in the negating function here:

mssql_not_sql_prefix <- function() {

I'll work on a PR for this

@edgararuiz edgararuiz self-assigned this Jun 18, 2018

@edgararuiz

This comment has been minimized.

Collaborator

edgararuiz commented Jun 19, 2018

Quick update, it looks like your suggestion fixes this issue, but it negatively impacts the custom MS SQL infix function we're using, so I have to work some more on this

mssql_logical_infix <- function(f) {

@bogdanrau

This comment has been minimized.

bogdanrau commented Jun 19, 2018

Thanks for your help on this @edgararuiz! We're transitioning from a MySQL db to MSSQL and it's been super smooth thus far minus this kink.

@ablack3

This comment has been minimized.

ablack3 commented Oct 5, 2018

Did this issue get worked out or is there a workaround? I'm having the same problem.

@edgararuiz

This comment has been minimized.

Collaborator

edgararuiz commented Oct 5, 2018

@ablack3 - Yes, I have a PR that fixes it. The PR is ready to be reviewed: #103

@hadley hadley closed this in #103 Jan 3, 2019

hadley added a commit that referenced this issue Jan 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment