-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
117 lines (85 loc) · 4.37 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# queryparser <img src="man/figures/logo.png" align="right" width="120" />
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/queryparser)](https://cran.r-project.org/package=queryparser)
[![Travis build status](https://travis-ci.com/ianmcook/queryparser.svg?branch=master)](https://travis-ci.com/ianmcook/queryparser)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/ianmcook/queryparser?branch=master&svg=true)](https://ci.appveyor.com/project/ianmcook/queryparser)
[![Codecov test coverage](https://codecov.io/gh/ianmcook/queryparser/branch/master/graph/badge.svg)](https://codecov.io/gh/ianmcook/queryparser?branch=master)
<!-- badges: end -->
**queryparser** translates SQL queries into lists of unevaluated R expressions.
| ⚠️ Most R users should not directly use queryparser. Instead, use it through [tidyquery](https://github.com/ianmcook/tidyquery).
| --- |
For an introduction to **tidyquery** and **queryparser**, watch the recording of the talk ["Bridging the Gap between SQL and R"](https://www.youtube.com/watch?v=JwP5KdWSgqE) from rstudio::conf(2020).
## Installation
Install the released version of **queryparser** from [CRAN](https://CRAN.R-project.org/package=queryparser) with:
``` r
install.packages("queryparser")
```
Or install the development version from [GitHub](https://github.com/ianmcook/queryparser) with:
``` r
# install.packages("remotes")
remotes::install_github("ianmcook/queryparser")
```
## Usage
Call the function `parse_query()`, passing a `SELECT` statement enclosed in quotes as the first argument:
```{r}
library(queryparser)
parse_query("SELECT DISTINCT carrier FROM flights WHERE dest = 'HNL'")
```
Queries can include the clauses `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`, and `LIMIT`:
```{r}
parse_query(
" SELECT origin, dest,
COUNT(flight) AS num_flts,
round(SUM(seats)) AS num_seats,
round(AVG(arr_delay)) AS avg_delay
FROM flights f LEFT OUTER JOIN planes p
ON f.tailnum = p.tailnum
WHERE distance BETWEEN 200 AND 300
AND air_time IS NOT NULL
GROUP BY origin, dest
HAVING num_flts > 3000
ORDER BY num_seats DESC, avg_delay ASC
LIMIT 2;"
)
```
Set the argument `tidyverse` to `TRUE` to use functions from [tidyverse](https://www.tidyverse.org) packages including [dplyr](https://dplyr.tidyverse.org), [stringr](https://stringr.tidyverse.org), and [lubridate](https://lubridate.tidyverse.org) in the R expressions:
```{r}
parse_query("SELECT COUNT(*) AS n FROM t WHERE x BETWEEN y AND z ORDER BY n DESC", tidyverse = TRUE)
```
**queryparser** will translate only explicitly allowed functions and operators, preventing injection of malicious code:
```{r error=TRUE}
parse_query("SELECT x FROM y WHERE system('rm -rf /')")
```
## Current Limitations
**queryparser** does not currently support:
- Subqueries
- Unions
- SQL-89-style (implicit) join notation
- The `WITH` clause (common table expressions)
- `OVER` expressions (window or analytic functions)
- Some SQL functions and operators
**queryparser** currently has the following known limitations:
- Some SQL expressions will translate only when `tidyverse` is set to `TRUE`. An example of this is `COUNT(DISTINCT ...)` expressions with multiple arguments.
- When logical operators (such as `IS NULL`) have unparenthesized expressions as their operands, R will interpret the resulting code using a different order of operations than a SQL engine would. When using an expression as the operand to a logical operator, always enclose the expression in parentheses.
- The error messages that occur when attempting to parse invalid or unrecognized SQL are often non-informative.
## Non-Goals
**queryparser** is not intended to:
- Translate other types of SQL statements (such as `INSERT` or `UPDATE`)
- Customize translations for specific SQL dialects
- Fully validate the syntax of the `SELECT` statements passed to it
- Efficiently process large batches of queries
- Facilitate the analysis of queries (for example, to identify patterns)
## Related Work
The **sqlparseR** package ([CRAN](https://cran.r-project.org/package=sqlparseR)) provides a wrapper around the Python module **sqlparse**.