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

Create nicer SQL #636

Closed
mgirlich opened this issue Apr 13, 2021 · 0 comments
Closed

Create nicer SQL #636

mgirlich opened this issue Apr 13, 2021 · 0 comments

Comments

@mgirlich
Copy link
Collaborator

The SQL queries generated by dbplyr are not very readable. Of course one can use one of the many available online SQL formatters but it would be still be very nice if the query itself would be more readable.

For example turn this

SELECT `x.x`, `x.y`, `y`
FROM (SELECT `x.x`, `x.y`, `y`, AVG(`x.x`) OVER (PARTITION BY `y`) AS `q01`
FROM (SELECT `LHS`.`x` AS `x.x`, `RHS`.`x` AS `x.y`, `y`
FROM `dbplyr_001` AS `LHS`
LEFT JOIN `dbplyr_002` AS `RHS`
ON (LHS.x < RHS.x)
))
WHERE (`q01` > 1.0)

into something like this

SELECT `x.x`,
       `x.y`,
       `y`
FROM
  (SELECT `x.x`,
          `x.y`,
          `y`,
          AVG(`x.x`) OVER (PARTITION BY `y`) AS `q01`
   FROM
     (SELECT `LHS`.`x` AS `x.x`,
             `RHS`.`x` AS `x.y`,
             `y`
      FROM `dbplyr_001` AS `LHS`
      LEFT JOIN `dbplyr_002` AS `RHS` ON (LHS.x < RHS.x)))
WHERE (`q01` > 1.0)

I looked around a little for existing R packages providing an SQL formatter but there seems to hardly any. I found sqlparseR which has quite a lot of extra Python dependencies.

dbplyr could directly generate pretty SQL code but this would probably require quite a bit of work and maintenance. So, it might be easier to create a wrapper around an existing SQL formatter.

@mgirlich mgirlich mentioned this issue Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant