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

Support "UPDATE" statement in "WITH" subquery #842

Merged
merged 3 commits into from Apr 9, 2023

Conversation

nicksrandall
Copy link
Contributor

@nicksrandall nicksrandall commented Mar 27, 2023

I was getting an error trying to parse this query with postgres:

WITH "result" AS (
  UPDATE
    "Hero"
  SET
    "name" = 'Captain America',
    "number_of_movies" = "number_of_movies" + 1
  WHERE
    "secret_identity" = 'Sam Wilson'
  RETURNING
    "id",
    "name",
    "secret_identity",
    "number_of_movies"
)
SELECT
  json_build_object('data', json_build_object('update', (
        SELECT
          json_agg("result")
        FROM "result")));

@ankrgyl
Copy link
Contributor

ankrgyl commented Mar 27, 2023

Thanks so much for the contribution @nicksrandall! Could you add some tests that demonstrate the new behavior?

src/parser.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@ankrgyl ankrgyl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, although the test could still be a bit better.

@alamb will leave it up to you on whether to push on that!


#[test]
fn test_update_in_with_subquery() {
let sql = r#"WITH "result" AS (UPDATE "Hero" SET "name" = 'Captain America', "number_of_movies" = "number_of_movies" + 1 WHERE "secret_identity" = 'Sam Wilson' RETURNING "id", "name", "secret_identity", "number_of_movies") SELECT * FROM "result""#;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally it's best to test across dialects, using the testing helpers (e.g. see test_parse_limit earlier in the file). In this case, the change itself has little to do with the dialect itself, so I'm not particularly concerned by it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree the tests are a little out of place here -- in general it seems like we have some tests in parser.rs that really belong in src/tests/...

I'll move them around in a follow on PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 4534413969

  • 25 of 25 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 86.299%

Totals Coverage Status
Change from base Build 4524349703: 0.01%
Covered Lines: 13926
Relevant Lines: 16137

💛 - Coveralls

@alamb
Copy link
Collaborator

alamb commented Apr 9, 2023

Thanks @nicksrandall and @ankrgyl

@alamb alamb merged commit 784a191 into sqlparser-rs:main Apr 9, 2023
9 checks passed
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 this pull request may close these issues.

None yet

4 participants