Skip to content

pmin() / pmax() aren't translated to SQL MIN() / MAX() #1711

@ghost

Description

I consider this a bug, and it feels like it should be an easy fix. In the meantime, I'd also be grateful if anybody could suggest a workaround: is it possible to mix SQL statements with dplyr verbs? I would hate to do my entire analysis in SQL. D:

library(dplyr)
set.seed(217)

# Put some fake data into a temporary SQLite DB
testDF <- data_frame(col_1 = sample.int(99, 8, replace = TRUE),
                     col_2 = sample.int(99, 8, replace = TRUE))
db <- src_sqlite(tempfile(), create = TRUE)
copy_to(db, testDF, "test", temporary = FALSE)
#>From: test [8 x 2]
#>
#>  col_1 col_2
#>  (int) (int)
#>1    42    74
#>2    15     5
#>3    26    23
#>4    83    98
#>5     6    62
#>6    66    18
#>7    69    27
#>8    80    14

# This would work if pmin() was translated
test_dplyr <- tbl(db, "test") %>%
  mutate(col_min = pmin(col_1, col_2))
print(test_dplyr)
#>Error in sqliteSendQuery(con, statement, bind.data) : 
#>  error in statement: no such function: PMIN

# This should work if min() was translated without checks
test_dplyr <- tbl(db, "test") %>%
  mutate(col_min = min(col_1, col_2))
#>Error: Invalid number of args to SQL MIN. Expecting 1

# Proof that one of these should work
test_sql <- dbGetQuery(db$con, 
                       "SELECT col_1, col_2, MIN(col_1, col_2) AS col_min
                        FROM test")
print(test_sql)
#>  col_1 col_2 col_min
#>1    42    74      42
#>2    15     5       5
#>3    26    23      23
#>4    83    98      83
#>5     6    62       6
#>6    66    18      18
#>7    69    27      27
#>8    80    14      14

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions