Skip to content

Conversation

NormallyGaussian
Copy link
Contributor

Brief summary of the change made

Fixes #6897. Adds MERGE to the supported list of statements that can come after a CTE. Added a test to verify the change.

Reference: https://www.datacamp.com/tutorial/cte-sql

I discovered this issue when trying to parse production Spark SQL code that is currently running.

Are there any other side effects of this change that we should be aware of?

N/A

Pull Request checklist

  • Please confirm you have completed any of the necessary steps below.

  • Included test cases to demonstrate any code changes, which may be one or more of the following:

    • .yml rule test cases in test/fixtures/rules/std_rule_cases.
    • .sql/.yml parser test cases in test/fixtures/dialects (note YML files can be auto generated with tox -e generate-fixture-yml).
    • Full autofix test cases in test/fixtures/linter/autofix.
    • Other.
  • Added appropriate documentation for the change.

  • Created GitHub issues for any relevant followup/future enhancements if appropriate.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Adds support for Common Table Expressions (CTEs) in MERGE statements for the ANSI SQL dialect, extending parser functionality for a common use case in Spark SQL.

  • Added MergeStatementSegment to NonWithNonSelectableGrammar in src/sqlfluff/dialects/dialect_ansi.py to enable CTE support for MERGE
  • Added comprehensive test coverage in test/fixtures/dialects/ansi/merge_into.sql demonstrating MERGE with CTEs
  • Extended test/fixtures/dialects/ansi/merge_into.yml to validate correct parsing of WITH clauses before MERGE statements

3 files reviewed, no comments
Edit PR Review Bot Settings | Greptile

@NormallyGaussian NormallyGaussian changed the title [ANSI] Support CTEs in Merge ANSI: Support CTEs in Merge Jun 25, 2025
Copy link
Contributor

github-actions bot commented Jun 25, 2025

Coverage Results ✅

Name    Stmts   Miss  Cover   Missing
-------------------------------------
TOTAL   19770      0   100%

249 files skipped due to complete coverage.

Copy link
Contributor

@keraion keraion left a comment

Choose a reason for hiding this comment

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

Makes sense. LGTM. Thanks!

@keraion keraion added this pull request to the merge queue Jun 29, 2025
Merged via the queue into sqlfluff:main with commit d5e23c8 Jun 29, 2025
28 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.

Spark: CTE in Merge Into
2 participants