Skip to content

R expression to SQL expression translation bug #634

@nassuphis

Description

@nassuphis

the expression ((1/(1 + abs(x))) - 1) * sign(x) does not seem to translate accurately into SQL under
certain conditions

in the below example, the expressions bug1 and bug2 are the same, the equality test returns TRUE.
expression bug1 produces different results in R and SQL
expression bug2 produces the same results in R and SQL

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(dbplyr)
#> Loading required package: dbplyr
#> 
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     ident, sql

bug_expr<-function(v)
{
  x <- enexpr(v)
  k  <- expr((1 / ( 1 + abs(!!x)))-1)
  expr(!!k*sign(!!x))
}

bug1<- bug_expr(x)
bug2<- expr(((1/(1 + abs(x))) - 1) * sign(x))

bug1==bug2
#> [1] TRUE

res <-
  memdb_frame(x=0:-3) %>%
  mutate(v_db=!!bug_expr(x)) %>%
  mutate(v1_db=!!bug1) %>%
  mutate(v2_db=!!bug2) %>%
  collect() %>% 
  mutate(v_r=!!bug_expr(x)) %>%
  mutate(v1_r=!!bug1) %>%
  mutate(v2_r=!!bug2)
  
all(res$v_db==res$v_r)
#> [1] FALSE
all(res$v1_db==res$v1_r)
#> [1] FALSE
all(res$v2_db==res$v2_r)
#> [1] TRUE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorfunc trans 🌍Translation of individual functions to SQL

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions