r4ds/import-databases.qmd

550 lines
23 KiB
Plaintext
Raw Normal View History

# Databases {#sec-import-databases}
2021-02-22 20:28:26 +08:00
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
2021-05-04 21:10:39 +08:00
status("drafting")
```
2022-05-11 06:26:04 +08:00
## Introduction
2022-05-14 02:38:55 +08:00
A huge amount of data lives in databases, and it's essential that as a data scientist you know how to access it.
2022-05-24 23:12:41 +08:00
Sometimes it's possible to get someone to download a snapshot into a .csv for you, but this is generally not desirable as the iteration speed is very slow.
2022-05-14 02:38:55 +08:00
You want to be able to reach into the database directly to get the data you need, when you need it.
2022-05-24 23:12:41 +08:00
In this chapter, you'll first learn the basics of the DBI package: how to use it to connect to a database and how to retrieve data by executing an SQL query.
**SQL**, short for **s**tructured **q**uery **l**anguage, is the lingua franca of databases, and is an important language for you to learn as a data scientist.
However, we're not going to start with SQL, but instead we'll teach you dbplyr, which can convert your dplyr code to the equivalent SQL.
We'll use that as way to teach you some of the most important features of SQL.
You won't become a SQL master by the end of the chapter, but you will be able to identify the most important components and understand what they do.
2022-05-14 02:38:55 +08:00
2022-05-24 23:12:41 +08:00
The main focus of this chapter, is working with data that already exists, data that someone else has collected in a database for you, as this represents the most common case.
But as we go along, we will point out a few tips and tricks for getting your own data into a database.
2022-05-11 06:26:04 +08:00
### Prerequisites
2022-05-24 23:12:41 +08:00
In this chapter, we'll add DBI and dbplyr into the mix.
DBI provides a low-level interface for connecting to databases and executing SQL.
dbplyr is a high-level interface that works with dplyr verbs to automatically generate SQL and then executes it using DBI.
```{r}
#| label: setup
#| message: false
2022-05-11 06:26:04 +08:00
library(DBI)
2022-05-24 23:12:41 +08:00
library(dbplyr)
2022-05-11 06:26:04 +08:00
library(tidyverse)
```
## Database basics
2022-05-24 23:12:41 +08:00
At the simplest level, you can think about a database as a collection of data frames, called **tables** in database terminology.
2022-05-11 06:26:04 +08:00
Like a data.frame, a database table is a collection of named columns, where every value in the column is the same type.
2022-05-24 23:12:41 +08:00
There are three high level differences between data frames and database tables:
2022-05-11 06:26:04 +08:00
- Database tables are stored on disk and can be arbitrarily large.
Data frames are stored in memory, and hence can't be bigger than your memory.
2022-05-24 23:12:41 +08:00
- Database tables usually have indexes.
Much like an index of a book, a database index makes it possible to find rows of interest without having to read every row.
2022-05-11 06:26:04 +08:00
Data frames and tibbles don't have indexes, but data.tables do, which is one of the reasons that they're so fast.
2022-05-24 23:12:41 +08:00
- Most classical databases are optimized for rapidly collecting data, not analyzing existing data.
These databases are called **row-oriented** because the data is stored row-by-row, rather than column-by-column like R.
More recently, there's been much development of **column-oriented** databases that make analyzing the existing data much faster.
Databases are run by database management systems (**DBMS** for short), which are typically run on a powerful central server.
Popular open source DBMS's of this nature are MariaDB, PostgreSQL, and SQL, and commercial equivalents include SQL Server and Oracle.
Today, many DBMS's run in the cloud, like Snowflake, Amazon's RedShift, and Google's BigQuery.
2022-05-11 06:26:04 +08:00
## Connecting to a database
2022-05-24 23:12:41 +08:00
To connect to the database from R, you'll use a pair of packages:
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
- You'll always use DBI (**d**ata**b**ase **i**nterface), provides a set of generic functions that perform connect to the database, upload data, run queries, and so on.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
- You'll also use a package specific to the DBMS you're connecting to.
This package translates the generic commands into the specifics needed for a given DBMS.
For example, if you're connecting to Postgres you'll use the RPostgres package.
If you're connecting to MariaDB or MySQL, you'll use the RMariaDB package.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
If you can't find a specific package for your DBMS, you can usually use the generic odbc package instead.
This uses the widespread ODBC standard.
odbc requires a little more setup because you'll also need to install and configure an ODBC driver.
Concretely, to create a database connection using `DBI::dbConnect()`.
The first argument specifies the DBMS and the second and subsequent arguments describe where the database lives and any credentials that you'll need to access it.
The following code shows are few typical examples:
2022-05-11 06:26:04 +08:00
```{r}
#| eval: false
2022-05-24 23:12:41 +08:00
con <- DBI::dbConnect(
RMariaDB::MariaDB(),
username = "foo"
)
con <- DBI::dbConnect(
RPostgres::Postgres(),
hostname = "databases.mycompany.com",
port = 1234
)
2022-05-11 06:26:04 +08:00
```
2022-05-24 23:12:41 +08:00
There's a lot of variation from DBMs to DBMS so unfortunately we can't cover all the details here.
So to connect the database you care about, you'll need to do a little research.
Typically you can ask the other data scientists in your team or talk to your DBA (**d**ata**b**ase **a**dministrator).
The initial setup will often take a little fiddling (and maybe some googling) to get right, but you'll generally only need to do it once.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
When you're done with the connection it's good practice to close it with `dbDisconnect(con)`.
This frees up resources on the database server for us by other people.
2022-05-14 02:38:55 +08:00
2022-05-11 06:26:04 +08:00
### In this book
2022-05-24 23:12:41 +08:00
Setting up a DBMS would be a pain for this book, so we'll instead use a self-contained DBMS that lives entirely in an R package: duckdb.
Thanks to the magic of DBI, the only difference between using duckdb and any other DBMS is how you'll connect to the database.
This makes it great to teach with because you can easily run this code as well as easily take what you learn and apply it elsewhere.
Connecting to duckdb is particularly simple because the defaults create a temporary database that is deleted when you quite R.
That's great for learning because it guarantees that you'll start from a clean slate every time you restart R:
2022-05-11 06:26:04 +08:00
```{r}
con <- DBI::dbConnect(duckdb::duckdb())
```
2022-05-24 23:12:41 +08:00
If you want to use duckdb for a real data analysis project[^import-databases-1], you'll also need to supply the `dbdir` argument to tell duckdb where to store the database files.
Assuming you're using a project (Chapter -@sec-workflow-scripts-projects)), it's reasonable to store it in the `duckdb` directory of the current project:
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
[^import-databases-1]: Which we highly recommend: it's a great database for data science.
```{r}
#| eval: false
2022-05-11 06:26:04 +08:00
con <- DBI::dbConnect(duckdb::duckdb(), dbdir = "duckdb")
```
duckdb is a high-performance database that's designed very much with the needs of the data scientist in mind, and the developers very much understand R and the types of real problems that R users face.
As you'll see in this chapter, it's really easy to get started with but it can also handle very large datasets.
2022-05-24 23:12:41 +08:00
### Load some data {#sec-load-data}
2022-05-11 06:26:04 +08:00
Since this is a temporary database, we need to start by adding some data.
2022-05-26 06:03:37 +08:00
Here we'll use the `mpg` and `diamonds` datasets from ggplot2.
2022-05-11 06:26:04 +08:00
```{r}
dbWriteTable(con, "mpg", ggplot2::mpg)
dbWriteTable(con, "diamonds", ggplot2::diamonds)
```
2022-05-24 23:12:41 +08:00
If you're using duckdb in a real project, I highly recommend learning about `duckdb_read_csv()` and `duckdb_register_arrow()`.
These give you powerful and performant ways to quickly load data directly into duckdb, without having to first load it in to R.
2022-05-14 02:38:55 +08:00
2022-05-11 06:26:04 +08:00
## Database basics
2022-05-14 02:38:55 +08:00
Now that we've connected to a database with some data in it, lets perform some basic operations with DBI.
### What's there?
2022-05-24 23:12:41 +08:00
The most important database objects for data scientists are tables.
DBI provides two useful functions to either list all the tables in the database[^import-databases-2] or to check if a specific table already exists:
[^import-databases-2]: At least, all the tables that you have permission to see.
2022-05-14 02:38:55 +08:00
```{r}
dbListTables(con)
dbExistsTable(con, "foo")
```
2022-05-11 06:26:04 +08:00
### Extract some data
2022-05-24 23:12:41 +08:00
Once you've determined a table exists, you can retrieve it with `dbReadTable()`:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-24 23:12:41 +08:00
con |>
dbReadTable("diamonds") |>
as_tibble()
2022-05-11 06:26:04 +08:00
```
2022-05-24 23:12:41 +08:00
`dbReadTable()` returns a `data.frame` so I use `as_tibble()` to convert it into a tibble so that it prints nicely.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
```{=html}
<!--
2022-05-14 02:38:55 +08:00
Notice something important with the diamonds dataset: the `cut`, `color`, and `clarity` columns were originally ordered factors, but now they're regular factors.
2022-05-24 23:12:41 +08:00
This particularly case isn't very important since ordered factors are barely different to regular factors, but it's good to know that the way that the database represents data can be slightly different to the way R represents data.
2022-05-14 02:38:55 +08:00
In this case, we're actually quite lucky because most databases don't support factors at all and would've converted the column to a string.
Again, not that important, because most of the time you'll be working with data that lives in a database, but good to be aware of if you're storing your own data into a database.
2022-05-24 23:12:41 +08:00
Generally you can expect numbers, strings, dates, and date-times to convert just fine, but other types may not.
-->
```
In real life, it's rare that you'll use `dbReadTable()` because the whole reason you're using a database is that there's too much data to fit in a data frame, and you want to use the database to bring back only a subset of the rows and columns.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
### Run a query {#sec-dbGetQuery}
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
The way you'll usually retrieve data is with `dbGetQuery()`.
It takes a database connection and some SQL code and returns a data frame:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-24 23:12:41 +08:00
con |>
dbGetQuery("
SELECT carat, cut, clarity, color, price
FROM diamonds
WHERE price > 15000
") |>
as_tibble()
2022-05-11 06:26:04 +08:00
```
2022-05-24 23:12:41 +08:00
Don't worry if you've never seen SQL code before as you'll learn more about it shortly.
But if read it carefully, you might guess that it selects five columns of the diamonds dataset and the rows where `price` is greater than 15,000.
2022-05-14 02:38:55 +08:00
You'll need to be a little careful with `dbGetQuery()` since it can potentially return more data than you have memory.
2022-05-24 23:12:41 +08:00
We won't discuss it further here, but if you're dealing with very large datasets it's possible to deal with a "page" of data at a time by using `dbSendQuery()` to get a "result set" which you can page through by calling `dbFetch()` until `dbHasCompleted()` returns `TRUE`.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
### Other functions
2022-05-14 02:38:55 +08:00
2022-05-24 23:12:41 +08:00
There are lots of other functions in DBI that you might find useful if you're managing your own data (like `dbWriteTable()` which we used in @sec-load-data), but we're going to skip past them in the interests of staying focused on working with data that already lives in a database.
2022-05-14 02:38:55 +08:00
2022-05-24 23:12:41 +08:00
## dbplyr basics
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
Now that you've learned the low-level basics for connecting to a database and running a query, we're going to switch it up a bit and learn a bit about dbplyr.
dbplyr is a dplyr **backend**, which means that you write the dplyr code that you're already familiar with and dbplyr translates it to run in a different way, in this case to SQL.
2022-05-14 02:38:55 +08:00
2022-05-26 06:03:37 +08:00
To use dbplyr, you must first use `tbl()` to create an object that represents a database table[^import-databases-3]:
2022-05-24 23:12:41 +08:00
[^import-databases-3]: If you want to mix SQL and dbplyr, you can also create a tbl from a SQL query with `tbl(con, SQL("SELECT * FROM foo")).`
2022-05-11 06:26:04 +08:00
```{r}
diamonds_db <- tbl(con, "diamonds")
diamonds_db
```
2022-05-26 06:03:37 +08:00
This object is **lazy**; when you use dplyr verbs on it, dplyr doesn't do any work: it just records the sequence of operations that you want to perform and only performs them when needed.
For example, take the following pipeline:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-14 02:38:55 +08:00
big_diamonds_db <- diamonds_db |>
filter(price > 15000) |>
select(carat:clarity, price)
2022-05-26 06:03:37 +08:00
2022-05-14 02:38:55 +08:00
big_diamonds_db
```
2022-05-26 06:03:37 +08:00
You can tell this object represents a database query because it prints the DBMS name at the top, and while it tells you the number of columns, it typically it won't tell you the number of rows.
This is because finding the total number of rows usually requires executing the complete query, something we're trying to avoid.
You can see the SQL the dbplyr generates by a dbplyr query by calling `show_query()`:
2022-05-14 02:38:55 +08:00
```{r}
big_diamonds_db |>
2022-05-11 06:26:04 +08:00
show_query()
```
2022-05-26 06:03:37 +08:00
To get all the data back into R, you call `collect()`.
Behind the scenes, this generates the SQL, calls `dbGetQuery()` to get the data, then turns the result into a tibble:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-14 02:38:55 +08:00
big_diamonds <- big_diamonds_db |>
2022-05-11 06:26:04 +08:00
collect()
big_diamonds
```
2022-05-24 23:12:41 +08:00
## SQL
2022-05-26 06:03:37 +08:00
The rest of the chapter will teach you a little about SQL through the lens of dbplyr.
It's a rather non-traditional introduction to SQL but I hope it will get you quickly up to speed with the basics.
It will hopefully help you understand the parallels between SQL and dplyr but it's not going to give you much practice writing SQL.
For that, I'd recommend [*SQL for Data Scientists*](https://sqlfordatascientists.com)by Renée M. P. Teate.
It's an introduction to SQL designed specifically for the needs of data scientists, and includes examples of the sort of highly interconnected data you're likely to encounter in real organisations.
2022-05-24 23:12:41 +08:00
2022-05-26 06:03:37 +08:00
We'll explore the relationship between dplyr and SQL using a couple of old friends from the nycflights dataset, the `flights` and `planes` tibbles.
These are easy to get into our learning database because dbplyr has a function designed for this exact scenario.
```{r}
dbplyr::copy_nycflights13(con)
flights <- tbl(con, "flights")
planes <- tbl(con, "planes")
2022-05-24 23:12:41 +08:00
```
2022-05-14 02:38:55 +08:00
### SQL basics
2022-05-26 06:03:37 +08:00
Instead of functions, like R, SQL has **statements**.
Common statements include `CREATE` for defining new tables, `INSERT` for adding data, and `SELECT` for retrieving data.
We're going to focus on `SELECT` statements, aka **queries**, because they are almost exclusively what you'll use as a data scientist.
Your job is usually to analyse existing data, and in most cases you won't even have permission to modify the data.
2022-05-14 02:38:55 +08:00
2022-05-26 06:03:37 +08:00
A query is made up of **clauses**.
2022-05-24 23:12:41 +08:00
Every query must have two clauses `SELECT` and `FROM`[^import-databases-4].
2022-05-26 06:03:37 +08:00
The simplest query is uses `SELECT * FROM tablename` to select columns from the specified table.
This is what dplyr generates for an adulterated table:
2022-05-14 02:38:55 +08:00
2022-05-24 23:12:41 +08:00
[^import-databases-4]: Ok, technically, only the `SELECT` is required, since you can write queries like `SELECT 1+1` to perform basic calculation.
2022-05-14 02:38:55 +08:00
But if you want to work with data (as you always do!) you'll also need a `FROM` clause.
2022-05-24 23:12:41 +08:00
```{r}
2022-05-26 06:03:37 +08:00
flights |> show_query()
planes |> show_query()
2022-05-24 23:12:41 +08:00
```
2022-05-26 06:03:37 +08:00
There are three other important clauses: `WHERE`, `ORDER BY`, and `GROUP BY`. `WHERE` and `ORDER BY` control which rows are included in the result and how they are ordered:
2022-05-11 06:26:04 +08:00
2022-05-26 06:03:37 +08:00
```{r}
flights |>
filter(dest == "IAH") |>
arrange(dep_delay) |>
show_query()
```
2022-05-14 02:38:55 +08:00
2022-05-26 06:03:37 +08:00
While `GROUP BY` works just like `dplyr::group_by()` causing aggregation to happen by group:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-26 06:03:37 +08:00
flights |>
group_by(dest) |>
summarise(dep_delay = mean(dep_delay, na.rm = TRUE)) |>
show_query()
2022-05-11 06:26:04 +08:00
```
2022-05-26 06:03:37 +08:00
There are two important differences between dplyr verbs and SELECT clauses:
- SQL, unlike R, is **case** **insensitive** so you can write `select`, `SELECT`, or even `SeLeCt`. In this book we'll stick with the common convention of writing SQL keywords in uppercase to distinguish them from table or variables names.
- In SQL, order matters. Unlike dplyr, where you can call the verbs in whatever order makes the most sense to you, SQL clauses must come in a specific order: `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `ORDER BY`. Confusingly, this order doesn't match how they are actually evaluated which is `FROM`, `WHERE`, `GROUP BY`, `SELECT`, `ORDER BY`.
The following sections will explore each clause in more detail.
### SELECT
`SELECT` is the workhorse of SQL queries, and is equivalent to `select()`, `mutate()`, `rename()`, `relocate()`, and, as you'll learn in the next section, `summarize()`.
`select()`, `rename()`, and `relocate()` have very direct translations to `SELECT` as they affect where a column appears (if at all) along with its name:
2022-05-14 02:38:55 +08:00
```{r}
2022-05-26 06:03:37 +08:00
flights |>
select(year:day, starts_with("dep")) |>
show_query()
flights |>
rename(tail_num = tailnum) |>
show_query()
flights |>
relocate(hour:minute, .after = day) |>
show_query()
2022-05-14 02:38:55 +08:00
```
2022-05-26 06:03:37 +08:00
This example also shows you have SQL does renaming.
In SQL terminology is called **aliasing** and is done with `AS`; just note that unlike with `mutate()`, the old name is on the left and the new name is on the right.
2022-05-14 02:38:55 +08:00
2022-05-26 06:03:37 +08:00
The translations for `mutate()` are similarly straightforward.
We'll come back to the translation of individual components in @sec-sql-expressions.
2022-05-14 02:38:55 +08:00
```{r}
diamonds_db |>
2022-05-26 06:03:37 +08:00
mutate(price_per_carat = price / carat) |>
2022-05-14 02:38:55 +08:00
show_query()
```
2022-05-11 06:26:04 +08:00
### GROUP BY
2022-05-26 06:03:37 +08:00
When pared with `group_by()`, `summarise()` is also translated to `SELECT`:
2022-05-11 06:26:04 +08:00
```{r}
diamonds_db |>
group_by(cut) |>
summarise(
n = n(),
2022-05-26 06:03:37 +08:00
avg_price = mean(price, na.rm = TRUE)
2022-05-11 06:26:04 +08:00
) |>
show_query()
```
2022-05-26 06:03:37 +08:00
We'll come back to the translations of `n()` and `mean()` in @sec-sql-expressions.
But it's no coincidence that `group_by()` is translated to `GROUP BY`: the SQL clause inspired the R function name.
### WHERE
`filter()` is translated to `WHERE`.
`|` becomes `OR` and `&` becomes `AND:`
```{r}
flights |>
filter(dest == "IAH" | dest == "HOU") |>
show_query()
flights |>
filter(arr_delay > 0 & arr_delay < 20) |>
show_query()
```
Note that SQL uses `=` for comparison, not `==`.
This is super annoying if you're switching between writing R code and SQL!
Another useful SQL function is `IN`, which is very close to R's `%in%`:
```{r}
flights |>
filter(dest %in% c("IAH", "HOU")) |>
show_query()
```
SQL doesn't have `NA`s, but instead has `NULL`s.
They behave very similarly to `NA`s, including their "infectious" properties.
```{r}
flights |>
filter(!is.na(dep_delay)) |>
show_query()
```
This SQL illustrates one of the drawbacks of dbplyr: it doesn't always generate the simplest SQL.
In this case, the parentheses are redundant and you could use the special form `IS NOT NULL` yielding:
2022-05-11 06:26:04 +08:00
2022-05-26 06:03:37 +08:00
``` sql
WHERE "dep_delay" IS NOT NULL
```
2022-05-11 06:26:04 +08:00
### ORDER BY
`arrange()` is translated to `ORDER BY`:
```{r}
2022-05-26 06:03:37 +08:00
flights |>
arrange(year, month, day, desc(dep_delay)) |>
2022-05-11 06:26:04 +08:00
show_query()
```
2022-05-26 06:03:37 +08:00
Note that `desc()` becomes `DESC`; this is another R function whose named was directly inspired by SQL.
2022-05-14 02:38:55 +08:00
2022-05-11 06:26:04 +08:00
### Subqueries
Some times it's not possible to express what you want in a single query.
For example, in `SELECT` can only refer to columns that exist in the `FROM`, not columns that you have just created.
So if you modify a column that you just created, dbplyr will need to create a subquery:
```{r}
diamonds_db |>
select(carat) |>
mutate(
carat2 = carat + 2,
carat3 = carat2 + 1
) |>
show_query()
```
A subquery is just a query that's nested inside of `FROM`, so instead of a table being used as the source, the new query is.
Another similar restriction is that `WHERE`, like `SELECT` can only operate on variables in `FROM`, so if you try and filter based on a variable that you just created, you'll need to create a subquery.
```{r}
diamonds_db |>
select(carat) |>
mutate(carat2 = carat + 2) |>
filter(carat2 > 1) |>
show_query()
```
Sometimes dbplyr uses a subquery where strictly speaking it's not necessary.
2022-05-26 06:03:37 +08:00
For example, take this pipeline that filters on a summary value.
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
### Joins
2022-05-11 06:26:04 +08:00
2022-05-26 06:03:37 +08:00
SQL joins are straightforward, but dbplyr's current translation requires spelling out
2022-05-11 06:26:04 +08:00
```{r}
2022-05-26 06:03:37 +08:00
flights |>
left_join(planes, by = "tailnum") |>
show_query()
2022-05-11 06:26:04 +08:00
```
2022-05-26 06:03:37 +08:00
Instead we'll create some dummy data:
2022-05-11 06:26:04 +08:00
```{r}
2022-05-26 06:03:37 +08:00
2022-05-11 06:26:04 +08:00
```
### Temporary data
Sometimes it's useful to perform a join or semi/anti join with data that you have locally.
How can you get that data into the database?
There are a few ways to do so.
You can set `copy = TRUE` to automatically copy.
There are two other ways that give you a little more control:
`copy_to()` --- this works very similarly to `DBI::dbWriteTable()` but returns a `tbl` so you don't need to create one after the fact.
By default this creates a temporary table, which will only be visible to the current connection (not to other people using the database), and will automatically be deleted when the connection finishes.
Most database will allow you to create temporary tables, even if you don't otherwise have write access to the data.
`copy_inline()` --- new in the latest version of db.
Rather than copying the data to the database, it builds SQL that generates the data inline.
It's useful if you don't have permission to create temporary tables, and is faster than `copy_to()` for small datasets.
2022-05-26 06:03:37 +08:00
### Other statements
in the case that you need to update your own database, you can solve most problems with `dbWriteTable()` and/or `dbInsertTable()`.
In fact, as a data scientist in most cases you won't even be able to run these statements because you only have read only access to the database.
This ensures that there's no way for you to accidentally mess things up.
## SQL expressions {#sec-sql-expressions}
2022-05-11 06:26:04 +08:00
2022-05-24 23:12:41 +08:00
https://dbplyr.tidyverse.org/articles/translation-function.html
2022-05-11 06:26:04 +08:00
Now that you understand the big picture of a SQL query and the equivalence between the SELECT clauses and dplyr verbs, it's time to look more at the details of the conversion of the individual expressions, i.e. what happens when you use `mean(x)` in a `summarize()`?
```{r}
dbplyr::translate_sql(a + 1)
```
2022-05-14 02:38:55 +08:00
- Most mathematical operators are the same.
The exception is `^`:
2022-05-11 06:26:04 +08:00
```{r}
dbplyr::translate_sql(1 + 2 * 3 / 4 ^ 5)
```
- In R strings are surrounded by `"` or `'` and variable names (if needed) use `` ` ``. In SQL, strings only use `'` and most databases use `"` for variable names.
```{r}
dbplyr::translate_sql(x == "x")
```
2022-05-14 02:38:55 +08:00
- In R, the default for a number is to be a double, i.e. `2` is a double and `2L` is an integer.
In SQL, the default is for a number to be an integer unless you put a `.0` after it:
2022-05-11 06:26:04 +08:00
```{r}
dbplyr::translate_sql(2 + 2L)
```
This is more important in SQL than in R because if you do `(x + y) / 2` in SQL it will use integer division.
- `ifelse()` and `case_when()` are translated to CASE WHEN:
```{r}
dbplyr::translate_sql(if_else(x > 5, "big", "small"))
```
- String functions
```{r}
dbplyr::translate_sql(paste0("Greetings ", name))
```
dbplyr also translates common string and date-time manipulation functions.
2022-05-24 23:12:41 +08:00
### SQL dialects
2022-05-11 06:26:04 +08:00
Note that every database uses a slightly different dialect of SQL.
For the vast majority of simple examples in this chapter, you won't see any differences.
But as you start to write more complex SQL you'll discover that what works on what database might not work on another.
Fortunately, dbplyr will take care a lot of this for you, as it automatically varies the SQL that it generates based on the database you're using.
It's not perfect, but if you discover the dbplyr creates SQL that works on one database but not another, please file an issue so we can try to make it better.
If you just want to see the SQL dbplyr generates for different databases, you can create a special simulated data frame.
This is mostly useful for the developers of dbplyr, but it also gives you an easy way to experiment with SQL variants.
```{r}
lf1 <- dbplyr::lazy_frame(name = "Hadley", con = dbplyr::simulate_oracle())
lf2 <- dbplyr::lazy_frame(name = "Hadley", con = dbplyr::simulate_postgres())
lf1 |>
mutate(greet = paste("Hello", name)) |>
head()
lf2 |>
mutate(greet = paste("Hello", name)) |>
head()
```