parse_query: Parse a SQL query

View source: R/parse_query.R

parse_queryR Documentation

Parse a SQL query

Description

Parses a SQL SELECT statement into a list with R expressions

Usage

parse_query(query, tidyverse = FALSE, secure = TRUE)

Arguments

query

a character string containing a SQL SELECT statement

tidyverse

set to TRUE to use functions from tidyverse packages including dplyr, stringr, and lubridate in the R expressions

secure

set to FALSE to allow potentially dangerous functions in the returned R expressions

Details

See the current limitations section of the README for information about what types of queries are supported.

Value

A list object with named elements representing the clauses of the query, containing sublists of unevaluated R expressions translated from the SQL expressions in the query.

Depending on the arguments, the returned list and its sublists will have attributes named distinct and aggregate with logical values that can aid in the evaluation of the R expressions. If query contains one or more joins, then the sublist named from will have attributes named join_types and join_conditions specifying the types of join and the join conditions.

See Also

parse_expression

Examples

my_query <- "SELECT origin, dest,
    COUNT(flight) AS num_flts,
    round(AVG(distance)) AS dist,
    round(AVG(arr_delay)) AS avg_delay
  FROM flights
  WHERE distance BETWEEN 200 AND 300
    AND air_time IS NOT NULL
  GROUP BY origin, dest
  HAVING num_flts > 3000
  ORDER BY num_flts DESC, avg_delay DESC
  LIMIT 100;"

parse_query(my_query)

parse_query(my_query, tidyverse = TRUE)

queryparser documentation built on Jan. 10, 2023, 1:08 a.m.