Skip to content

MSSQL Incorrect Translation of between() when not used in filter context #1241

@kmishra9

Description

@kmishra9

Hey there, stumbling into some more issues related to SQL translations w/ booleans in MSSQL. In this case, the translation for dplyr::between() is the same in both a mutate/summarize context (which doesn't work) and in a filter context (which does).

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(dbplyr))
suppressPackageStartupMessages(library(lubridate))

lf <-
    lazy_frame(tibble(
        a = 1:10,
        b = 2
    ),
    con = simulate_mssql())

# Succeeds (stock translation)
lf %>% filter(between(x = a, left = b, right = 5))
#> <SQL>
#> SELECT *
#> FROM `df`
#> WHERE (`a` BETWEEN `b` AND 5.0)

# Fails (stock translation)
lf %>% mutate(c = between(x = a, left = b, right = 5))
#> <SQL>
#> SELECT *, `a` BETWEEN `b` AND 5.0 AS `c`
#> FROM `df`

# Succeeds (possible new translation)
lf %>% mutate(c = case_when(between(
    x = a, left = b, right = 3
) ~ TRUE, .default = FALSE))
#> <SQL>
#> SELECT *, CASE WHEN (`a` BETWEEN `b` AND 3.0) THEN 1 ELSE 0 END AS `c`
#> FROM `df`

# Succeeds (possible new translation)
lf %>% mutate(c = if_else(condition = between(
    x = a, left = b, right = 3
), true = TRUE, false = FALSE))
#> <SQL>
#> SELECT *, IIF(`a` BETWEEN `b` AND 3.0, 1, 0) AS `c`
#> FROM `df`

Created on 2023-04-10 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions