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

Added ifelse -> IF() function mapping #53

Merged
merged 1 commit into from Jun 16, 2016

Conversation

@realAkhmed
Copy link
Contributor

commented Jul 1, 2015

This pull request adds ifelse function SQL translation.

Google BigQuery has a direct equivalent to R's ifelse function which is called IF.
According to reference, the function is in a category called "Other functions" and accepts 3 arguments just like ifelse does.

An example test case is listed below

library(dplyr)
library(bigrquery)

BILLING_PROJECT <- "<YOUR_BILLING_PROJECT>"
BILLING_DATASET <- "<DATASET_INSIDE_THAT_PROJECT>"

bq <- src_bigquery(BILLING_PROJECT, BILLING_DATASET)

# Upload the test data frame to your project
df <- data.frame(a = 1:100, b = 101:200)
ifelse_test_df <- copy_to(bq, df, name = "ifelse_test_df")

# Perform a simple mutate query with ifelse
ifelse_test_df %>%
  mutate( z = ifelse(a <= 10L, 1L, 0L) ) %>%
  summarise( sum_z = sum(z) ) %>%
  collect

The use of function with NAs as one of the arguments requires one to specify the type of NA.

# Perform a simple mutate query with ifelse and NULLs
ifelse_test_df %>%
  mutate( z = ifelse(a <= 10L, 1L, INTEGER(NULL)) ) %>%
  summarise( sum_z = sum(z) ) %>%
  collect

Please note that running the test does require to have a Google Cloud billing project set up already.

Thank you for consideration!

@craigcitro

This comment has been minimized.

Copy link
Collaborator

commented Jul 3, 2015

this LGTM, but (in lieu of a test) i wonder if we should add an example somewhere, so that it's clear this is supported?

@hadley -- is there a common way people document what all is supported by a given dplyr backend?

@hadley

This comment has been minimized.

Copy link
Member

commented Jul 4, 2015

@craigcitro no, but there should be :/

@realAkhmed

This comment has been minimized.

Copy link
Contributor Author

commented Jul 5, 2015

Happy to add an example but it is also not clear to me where to put it.

@hadley hadley merged commit bdc1223 into r-dbi:master Jun 16, 2016

2 checks passed

code-review/gitcolony 0/0 qa 0/0 dev, 0 issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@hadley

This comment has been minimized.

Copy link
Member

commented Jun 16, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.