{"id":13857621,"url":"https://github.com/ianmcook/queryparser","last_synced_at":"2025-04-14T22:50:55.173Z","repository":{"id":56935313,"uuid":"202947077","full_name":"ianmcook/queryparser","owner":"ianmcook","description":"Translate SQL queries into R expressions","archived":false,"fork":false,"pushed_at":"2023-01-09T21:38:36.000Z","size":530,"stargazers_count":52,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2023-11-20T16:31:02.704Z","etag":null,"topics":["database","r","sql","tidyverse"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ianmcook.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-18T01:29:26.000Z","updated_at":"2023-11-07T00:25:11.000Z","dependencies_parsed_at":"2023-02-08T14:30:26.195Z","dependency_job_id":null,"html_url":"https://github.com/ianmcook/queryparser","commit_stats":null,"previous_names":[],"tags_count":5,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianmcook%2Fqueryparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianmcook%2Fqueryparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianmcook%2Fqueryparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianmcook%2Fqueryparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ianmcook","download_url":"https://codeload.github.com/ianmcook/queryparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975299,"owners_count":21192199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","r","sql","tidyverse"],"created_at":"2024-08-05T03:01:42.164Z","updated_at":"2025-04-14T22:50:55.137Z","avatar_url":"https://github.com/ianmcook.png","language":"R","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n# queryparser \u003cimg src=\"man/figures/logo.png\" align=\"right\" width=\"120\" /\u003e\n\n\n\u003c!-- badges: start --\u003e\n[![CRAN status](https://www.r-pkg.org/badges/version/queryparser)](https://cran.r-project.org/package=queryparser)\n[![Travis build status](https://travis-ci.com/ianmcook/queryparser.svg?branch=master)](https://travis-ci.com/ianmcook/queryparser)\n[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/ianmcook/queryparser?branch=master\u0026svg=true)](https://ci.appveyor.com/project/ianmcook/queryparser)\n[![Codecov test coverage](https://codecov.io/gh/ianmcook/queryparser/branch/master/graph/badge.svg)](https://codecov.io/gh/ianmcook/queryparser?branch=master)\n\u003c!-- badges: end --\u003e\n\n**queryparser** translates SQL queries into lists of unevaluated R expressions.\n\n| ⚠️ Most R users should not directly use queryparser. Instead, use it through [tidyquery](https://github.com/ianmcook/tidyquery).\n| --- |\n\nFor 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).\n\n## Installation\n\nInstall the released version of **queryparser** from [CRAN](https://CRAN.R-project.org/package=queryparser) with:\n\n``` r\ninstall.packages(\"queryparser\")\n```\n\nOr install the development version from [GitHub](https://github.com/ianmcook/queryparser) with:\n\n``` r\n# install.packages(\"remotes\")\nremotes::install_github(\"ianmcook/queryparser\")\n```\n\n## Usage\n\nCall the function `parse_query()`, passing a `SELECT` statement enclosed in quotes as the first argument:\n\n```{r}\nlibrary(queryparser)\n\nparse_query(\"SELECT DISTINCT carrier FROM flights WHERE dest = 'HNL'\")\n```\n\nQueries can include the clauses `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`, and `LIMIT`:\n\n```{r}\nparse_query(\n\" SELECT origin, dest,\n    COUNT(flight) AS num_flts,\n    round(SUM(seats)) AS num_seats,\n    round(AVG(arr_delay)) AS avg_delay\n  FROM flights f LEFT OUTER JOIN planes p\n    ON f.tailnum = p.tailnum\n  WHERE distance BETWEEN 200 AND 300\n    AND air_time IS NOT NULL\n  GROUP BY origin, dest\n  HAVING num_flts \u003e 3000\n  ORDER BY num_seats DESC, avg_delay ASC\n  LIMIT 2;\"\n)\n```\n\nSet 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:\n\n```{r}\nparse_query(\"SELECT COUNT(*) AS n FROM t WHERE x BETWEEN y AND z ORDER BY n DESC\", tidyverse = TRUE)\n```\n\n**queryparser** will translate only explicitly allowed functions and operators, preventing injection of malicious code:\n\n```{r error=TRUE}\nparse_query(\"SELECT x FROM y WHERE system('rm -rf /')\")\n```\n\n## Current Limitations\n\n**queryparser** does not currently support:\n\n- Subqueries\n- Unions\n- SQL-89-style (implicit) join notation\n- The `WITH` clause (common table expressions)\n- `OVER` expressions (window or analytic functions)\n- Some SQL functions and operators\n\n**queryparser** currently has the following known limitations:\n\n- Some SQL expressions will translate only when `tidyverse` is set to `TRUE`. An example of this is `COUNT(DISTINCT ...)` expressions with multiple arguments.\n- 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.\n- The error messages that occur when attempting to parse invalid or unrecognized SQL are often non-informative.\n\n## Non-Goals\n\n**queryparser** is not intended to:\n\n- Translate other types of SQL statements (such as `INSERT` or `UPDATE`)\n- Customize translations for specific SQL dialects\n- Fully validate the syntax of the `SELECT` statements passed to it\n- Efficiently process large batches of queries\n- Facilitate the analysis of queries (for example, to identify patterns)\n\n## Related Work\n\nThe **sqlparseR** package ([CRAN](https://cran.r-project.org/package=sqlparseR)) provides a wrapper around the Python module **sqlparse**.\n","funding_links":[],"categories":["R"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianmcook%2Fqueryparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fianmcook%2Fqueryparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianmcook%2Fqueryparser/lists"}