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

bracket filter in summarize not work in MS SQL after update 2.0.0 #546

Closed
coponhub opened this issue Nov 19, 2020 · 3 comments
Closed

bracket filter in summarize not work in MS SQL after update 2.0.0 #546

coponhub opened this issue Nov 19, 2020 · 3 comments
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL

Comments

@coponhub
Copy link

bracket filter not work on dbplyr 2.0.0 on MS SQL like berow
tbl(con, "HOGE") %>% group_by(City_ID) %>% summarise(userVisit = n_distinct(visit_id[isUser == 1L]))

Error: nanodbc/nanodbc.cpp:1617: 00000: [FreeTDS][SQL Server]Statement(s) could not be prepared. [FreeTDS][SQL Server]An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
'SELECT TOP 11 "City_ID", COUNT(DISTINCT CASE WHEN ("isUser") THEN ("visit_id") END) AS "usrVisit"
FROM "HOGE"
GROUP BY "City_ID"'

It worked on dbplyr 1.4.4

@hadley

This comment has been minimized.

@hadley hadley added the reprex needs a minimal reproducible example label Nov 19, 2020
@coponhub
Copy link
Author

2.0.0

library(tidyverse)
dbplyr::lazy_frame(x = 1:5, y = 5:1, con = dbplyr::simulate_mssql()) %>%
    summarize(z = n_distinct(x[x > y])) %>%
    show_query()
#> <SQL>
#> SELECT COUNT(DISTINCT CASE WHEN (CAST(IIF(`x` > `y`, 1, 0) AS BIT)) THEN (`x`) END) AS `z`
#> FROM `df`
dbplyr::lazy_frame(x = 1:5, y = 5:1, con = dbplyr::simulate_mssql()) %>%
    summarize(z = sum(x[x > y], na.rm = TRUE)) %>%
    show_query()
#> <SQL>
#> SELECT SUM(CASE WHEN (CAST(IIF(`x` > `y`, 1, 0) AS BIT)) THEN (`x`) END) AS `z`
#> FROM `df`

SUM(CASE WHEN (CAST(IIF(`x` > `y`, 1, 0) AS BIT)) THEN (`x`) END)

An expression of non-boolean type(BIT) specified in a context where a condition is expected.

But it seem correct SQL generate in 1.4.4

1.4.4

library(tidyverse)
dbplyr::lazy_frame(x = 1:5, y = 5:1, con = dbplyr::simulate_mssql()) %>%
    summarize(z = n_distinct(x[x > y])) %>%
    show_query()
#> <SQL>
#> SELECT COUNT(DISTINCT CASE WHEN (`x` > `y`) THEN (`x`) END) AS `z`
#> FROM `df`
dbplyr::lazy_frame(x = 1:5, y = 5:1, con = dbplyr::simulate_mssql()) %>%
    summarize(z = sum(x[x > y], na.rm = TRUE)) %>%
    show_query()
#> <SQL>
#> SELECT SUM(CASE WHEN (`x` > `y`) THEN (`x`) END) AS `z`
#> FROM `df`

Created on 2020-11-24 by the reprex package (v0.3.0)

@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL and removed reprex needs a minimal reproducible example labels Jan 21, 2021
@hadley hadley closed this as completed in 897a38d Jan 21, 2021
@hadley
Copy link
Member

hadley commented Jan 21, 2021

Thanks! Please let me know if you spot any other places where the translation fails — it's not possible to get it 100% correct, but we should be able to catch the problems in most real world SQL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL
Projects
None yet
Development

No branches or pull requests

2 participants