apache / apache/datafusion-sqlparser-rs

MSSQL: Issue with CTE anchors

Open
#1,537 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.5k
Forks
772
Avg merge
4d 9h
Merged PRs (30d)
17

Description

Hi, the following is a valid MSSQL syntax

When parsed with the `sql-parser-rs`

```sql
WITH RecursiveCTE AS (
-- Anchor member
SELECT TOP 1
[ID],
CAST([Amount] AS NUMERIC) AS [Amount],
CAST([Balance] AS NUMERIC) AS [Balance],
(SELECT TOP 1 [FixedValue] FROM [Settings] ORDER BY [ID] DESC) AS [Constant]
FROM [Transactions]
ORDER BY [ID] ASC

UNION ALL

-- Recursive member
SELECT
[ID] + 1 AS [ID],
CAST([Amount] * 0.8 AS NUMERIC) AS [Amount],
CAST(([Amount] * 0.8 - [Constant]) AS NUMERIC) AS [Balance],
[Constant]
FROM RecursiveCTE
WHERE [Balance] > 0
)
SELECT [ID], [Balance]
FROM RecursiveCTE
ORDER BY [ID] ASC;
```

it gives the following error

```bash
sql parser error: Expected: ), found: UNION at Line: 11, Column: 5
```

https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the supplied MSSQL CTE query in sql-parser-rs and compare the parser's CTE handling with the linked Microsoft documentation. Trace the parser entry point that reports the unexpected UNION, then add a regression test showing that the query parses successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.